iPixel Posted April 20, 2009 Share Posted April 20, 2009 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 More sharing options...
MasterACE14 Posted April 20, 2009 Share Posted April 20, 2009 I believe at the end of the query. this... LIMIT 0, 20 needs to be this... LIMIT 20 forgive me if I'm wrong. Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814534 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2009 Share Posted April 20, 2009 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. Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814536 Share on other sites More sharing options...
iPixel Posted April 20, 2009 Author Share Posted April 20, 2009 Nope that didnt make a difference. But thanks. Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814537 Share on other sites More sharing options...
iPixel Posted April 20, 2009 Author Share Posted April 20, 2009 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? Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814540 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2009 Share Posted April 20, 2009 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 />"; } Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814544 Share on other sites More sharing options...
iPixel Posted April 20, 2009 Author Share Posted April 20, 2009 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 Link to comment https://forums.phpfreaks.com/topic/154873-solved-why-is-my-first-record-empty/#findComment-814552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.