Jump to content

Recommended Posts

My MySQL Version is 5.1 and I am getting no errors.

 

I am trying to generate a leaderboards and then displaying the output in a HTML table, here is my code:

 

<?php
    //I removed these but in the actual script they are working
/** MySQL database name */
define('DB_NAME', '');
/** MySQL database username */
define('DB_USER', '');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', '');

$table = "highscores";

// Initialization
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);

// Error checking
if(!$conn) {
	die('Error: ' . mysql_error());
}

$type   = isset($_GET['type']) ? $_GET['type'] : "global";
$offset = isset($_GET['offset']) ? $_GET['offset'] : "0";
$count  = isset($_GET['count']) ? $_GET['count'] : "10";
$sort   = isset($_GET['sort']) ? $_GET['sort'] : "Credits DESC";

// Localize the GET variables
$user   = isset($_GET['username']) ? $_GET['username'] : "";
$time   = isset($_GET['time']) ? $_GET['time']  : "";
$videos  = isset($_GET['videos']) ? $_GET['videos'] : "";
$credits  = isset($_GET['credits']) ? $_GET['credits'] : "";

// Protect against sql injections
$type   = mysql_real_escape_string($type);
$offset = mysql_real_escape_string($offset);
$count  = mysql_real_escape_string($count);
$sort   = mysql_real_escape_string($sort);
$user  = mysql_real_escape_string($user);
$time  = mysql_real_escape_string($time);
$videos = mysql_real_escape_string($videos);
        $credits = mysql_real_escape_string($credits);

// Build the sql query
$sql = "SELECT * FROM $table WHERE ";

switch($type) {
	case "global":
		$sql .= "1 ";
		break;
	case "username":
		$sql .= "Username = '$user' ";
		break;
	case "videos":
		$sql .= "Videos = '$videos' ";
		break;
	case "time":
		$sql .= "Time = '$time' ";
		break;
	case "credits":
		$sql .= "Credits = '$credits' ";
		break;
}

$sql .= "ORDER BY $sort ";
$sql .= "LIMIT $offset,$count ";

$result = mysql_query($sql,$conn);

if(!$result) {
	die("Error retrieving scores " . mysql_error());
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
</head>
<body>
        <center>
	<?php
		// Display the table
		echo '<table style="width:25%">
                        <table border="1">
				<thead>
					<tr>
						<th>Username</th>
						<th>Time Ran</th>
						<th>Videos Watched</th>
						<th>Credits Gained</th>
					</tr>
				</thead>
				<tbody>';
	    while ($row = mysql_fetch_object($result)) {
			echo '<tr>
					<td>
					'.$row->username.'
					</td>
					<td>
					'.$row->time.'
					</td>
				        <td>
					'.$row->videos.'
					</td>
				         <td>
					'.$row->credits.'
					</td>
				  </tr>';
		}
		echo '</tbody>
			</table>';
		mysql_free_result($result);
		mysql_close($conn);
	?>
</center>
</body>
</html>

 

I have 3 different entries I inserted into the database but it is generating nothing:

ewh8y.png

 

I am not quite sure what's wrong and if anyone can help me that would be great, thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/245015-html-table-not-displaying-anything/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.