Jump to content

[SOLVED] Why is my first record empty ?


iPixel

Recommended Posts

Im using a do/while loop to show all my retreived records.

But for some reason my first record is empty, and i know im not missing any records or it's not skipping but for some reason my first echo is empty.

 

here's my code - what am i missing ?

$SP_query = "SELECT * FROM table WHERE firstname LIKE '%$q%' OR lastname LIKE '%$q%' LIMIT 0, 20";
$SP_sql = mysql_query($SP_query) or die(mysql_error());

do
{
	$makenameforJS = $SP_result['firstname'] . " " . $SP_result['lastname'];
	$display_string.= "<span class=\"emp_list\"><a href=\"javascript:void(0)\" onClick=\"placeValue('" . $makenameforJS . "')\">" . $SP_result['firstname'] . " " . $SP_result['lastname'] . " - " . $SP_result['phone'] . "</a></span><br />";
}
while($SP_result = mysql_fetch_assoc($SP_sql));

 

so the result should be lets say...

 

James Brown - Ext. 1111

Frank Bent - Ext. 2222

etc...

 

but what i get is

-

James Brown - Ext. 1111

Frank Bent - Ext. 2222

 

Why am i getting that first (  -  )

 

Thanks, and my apologies for the really NOOB question.

Link to comment
https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/
Share on other sites

It's empty because you don't fetch a row from the result set until the end of the do/while loop. This is why do/while loops are almost never used. You must add more code to test if there is data and fetch the first row before the loop starts. Using a While loop eliminates the need for the extra conditional test and fetch instruction, resulting in shorter, faster, and clearer code.

It's empty because you don't fetch a row from the result set until the end of the do/while loop. This is why do/while loops are almost never used. You must add more code to test if there is data and fetch the first row before the loop starts. Using a While loop eliminates the need for the extra conditional test and fetch instruction, resulting in shorter, faster, and clearer code.

 

Ok that makes sense, but im not really sure how to put that check in? Or should i just use a different loop to save myself the time?

The following two pieces of code are logically equal, which one would you use -

 

if($SP_result = mysql_fetch_assoc($SP_sql)){
do
{
	$makenameforJS = $SP_result['firstname'] . " " . $SP_result['lastname'];
	$display_string.= "<span class=\"emp_list\"><a href=\"javascript:void(0)\" onClick=\"placeValue('" . $makenameforJS . "')\">" . $SP_result['firstname'] . " " . $SP_result['lastname'] . " - " . $SP_result['phone'] . "</a></span><br />";
}
while($SP_result = mysql_fetch_assoc($SP_sql));
}

 

while($SP_result = mysql_fetch_assoc($SP_sql)){
$makenameforJS = $SP_result['firstname'] . " " . $SP_result['lastname'];
$display_string.= "<span class=\"emp_list\"><a href=\"javascript:void(0)\" onClick=\"placeValue('" . $makenameforJS . "')\">" . $SP_result['firstname'] . " " . $SP_result['lastname'] . " - " . $SP_result['phone'] . "</a></span><br />";
}

The following two pieces of code are logically equal, which one would you use -

 

if($SP_result = mysql_fetch_assoc($SP_sql)){
do
{
	$makenameforJS = $SP_result['firstname'] . " " . $SP_result['lastname'];
	$display_string.= "<span class=\"emp_list\"><a href=\"javascript:void(0)\" onClick=\"placeValue('" . $makenameforJS . "')\">" . $SP_result['firstname'] . " " . $SP_result['lastname'] . " - " . $SP_result['phone'] . "</a></span><br />";
}
while($SP_result = mysql_fetch_assoc($SP_sql));
}

 

while($SP_result = mysql_fetch_assoc($SP_sql)){
$makenameforJS = $SP_result['firstname'] . " " . $SP_result['lastname'];
$display_string.= "<span class=\"emp_list\"><a href=\"javascript:void(0)\" onClick=\"placeValue('" . $makenameforJS . "')\">" . $SP_result['firstname'] . " " . $SP_result['lastname'] . " - " . $SP_result['phone'] . "</a></span><br />";
}

 

That second one did the trick nice and easy. thanks for the help

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.