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
https://forums.phpfreaks.com/topic/59335-not-returning-first-row/
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.

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";
     }
   }
 }

?>

 

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!

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";

	}
}
?>

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.