Jump to content

Not returning first row


jayshadow

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

 

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
Share on other sites

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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