MySQL Query -> DataTable Issues

Hey.

I’ve got an issue with my script. The Data is pulled from the database, but for some reason the printed Data skips 1 Row, which is not what it’s supposed to do. It pulls the rest of them quite successfully, but the fact that it skips 1 ruins the whole point of the script.

Here’s a picture of the Database Table Data, currently:

Here’s a picture of the Printed Data on the website:

And here’s the code

[php] if($_SESSION[‘SESS_MEMBER_ID’] == $data[‘uid’]) {
$sql = “SELECT * FROM grannar where uid=’”.$data[‘uid’]."’ ORDER BY targetname";
$query = mysql_query( $sql );
$rows = mysql_nuM_rows( $query );

$data = NULL;
echo ‘

’;
echo ‘
’;
echo ’ - Mina Grannar -’;
echo ‘
’;
if($rows == 0) {
echo ‘Du har tyvärr inga grannar ännu.’;
}
			if($query) {

			$row = mysql_fetch_array($query);
			$table_start  = "<table border='0' style='color: #8e0046;'>
"; $table_end = ""; while($row = mysql_fetch_array($query)) { $data .= ""; $data .= " "; if($row['connected'] == "online") { $data .= " " . ucfirst($row['targetname']) . " "; } else { $data .= " " . ucfirst($row['targetname']) . " "; } //echo ' '.$fname .' ' .$lastname. ''; $data .= " "; } echo $table_start . $data . $table_end; echo '
'; } }[/php]

Why is your code so disorganized?

The first instance of $row = mysql_fetch_array($query); isn’t needed, its not doing anything but taking up memory with useless information.

Since its not obvious as to why its not reporting all the entries, lets start with verifying how many it is finding. So, since you’re already seeing how many records are being returned, do this

$rows = mysql_num_rows($query); // don’t like extra spaces in my code, sorry
echo $rows."
";

If it echos 6, then the problem is somewhere else, if it comes up with 5 or less, then the problem is with the query.

Also, you’re already using an if statement to do something if the query is empty, instead of doing if($query), just do

if($rows == 0) {
// echo statement
} else {
// rest of code
}

Its one less thing that you have to troubleshoot later on.

You don’t need 3 different variables for the table, just use 1.

$table .= table start
// while loop
$table .= cells
// end loop
$table .= table end

Sponsor our Newsletter | Privacy Policy | Terms of Service