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"; } } Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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"; } } } ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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"; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.