jayshadow Posted July 10, 2007 Share Posted July 10, 2007 I have the following code, and it's connecting and pulling the information, however, when only one line is returned, it's not displaying it. Though if I have 2 rows, it will display the 2nd row but not the first. $sql = "SELECT * FROM ipsr_garage WHERE userid = $userid"; $result = $db->sql_query($sql); if (!$row = $db->sql_fetchrow($result)) { echo "You currently have no cars in your garage."; } else { while ($row = $db->sql_fetchrow($result)) { $year = $row['year']; $make = $row['make']; $model = $row['model']; echo "$year $make $model"; } } Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/ Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 first try this prnit_r($row); to know whether the error is on the class or in the loop Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294768 Share on other sites More sharing options...
Carterhost Posted July 10, 2007 Share Posted July 10, 2007 use this instead: $sql = "SELECT * FROM ipsr_garage WHERE userid = $userid"; $result = $db->sql_query($sql); $num_rows = mysql_num_rows($result); if ($numrows == 0) { echo "You currently have no cars in your garage."; }else { while ($row = $db->sql_fetchrow($result)) { $year = $row['year']; $make = $row['make']; $model = $row['model']; echo "$year $make $model"; } } It counts the number of results, instead of pulling the first result for the test. Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294771 Share on other sites More sharing options...
trq Posted July 10, 2007 Share Posted July 10, 2007 Each time you call $db->sql_fetchrow() it fetches the current row then moves the pointer forward. Look at your code, you call $db->sql_fetchrow() before your loop. I don't know what database object your using... but does it have a numrows() or simular method? Something like.... <?php $sql = "SELECT * FROM ipsr_garage WHERE userid = $userid"; if ($result = $db->sql_query($sql)) { if (!$row = $db->sql_num_rows($result)) { // you need a num_rows or simular method. echo "You currently have no cars in your garage."; } else { while ($row = $db->sql_fetchrow($result)) { $year = $row['year']; $make = $row['make']; $model = $row['model']; echo "$year $make $model"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294773 Share on other sites More sharing options...
Carterhost Posted July 10, 2007 Share Posted July 10, 2007 I don't know what database object your using... but does it have a numrows() or simular method? Something like.... Yeah, Couldn't even find matching commands in the php manual. Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294775 Share on other sites More sharing options...
teng84 Posted July 10, 2007 Share Posted July 10, 2007 I don't know what database object your using... but does it have a numrows() or simular method? Something like.... Yeah, Couldn't even find matching commands in the php manual. ???? what the commands is inside the class Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294776 Share on other sites More sharing options...
trq Posted July 10, 2007 Share Posted July 10, 2007 Yeah, Couldn't even find matching commands in the php manual. Because the op is not using a standard php class or functions. Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294777 Share on other sites More sharing options...
Carterhost Posted July 10, 2007 Share Posted July 10, 2007 I don't know what database object your using... but does it have a numrows() or simular method? Something like.... Yeah, Couldn't even find matching commands in the php manual. ???? what the commands is inside the class Eek, My Bad. Edit... it's too late for me. Roll on 1am when I can finish work! Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294778 Share on other sites More sharing options...
dewey_witt Posted July 11, 2007 Share Posted July 11, 2007 Try This <?php $sql = "SELECT * FROM ipsr_garage WHERE userid = $userid"; $result= mysql_query($sql, $connection) or die (mysql_error()); $result= mysql_query($sql, $connection) or die (mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $year = $row['year']; $make = $row['make']; $model = $row['model']; echo "$year $make $model"; } } ?> Link to comment https://forums.phpfreaks.com/topic/59335-not-returning-first-row/#findComment-294886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.