Jump to content


Photo

Missing SQL row


  • Please log in to reply
7 replies to this topic

#1 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 16 July 2006 - 10:24 PM

Here is my code...

$sql = 'SELECT * FROM `tbl_category` WHERE `cat_parent_id` = 0 ';
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

            <select name="mainCategory" id="mainCategory">
<option value = "0" selected> -- None -- </option>
<?php
while ($row = mysql_fetch_array($result))
{
print ( "<option value = " );
printf ($row["cat_id"]);
print ( ">" );
printf ($row["cat_name"]);
print ( "</option>\n" );
}
?>
    </select>

Now the Query returns 9 rows, however, only 8 options (+ "None", making 9 options) show up in the drop down list.
Why??? ???

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 16 July 2006 - 10:28 PM

I honestly don't know what "printf" does, but here's a shot...

$query = "SELECT * FROM tbl_category WHERE cat_parent_id=0";
$result = mysql_query($query);
echo '<select name="mainCategory" id="mainCategory">
<option value="0" selected>-- NONE --</option>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo '<option value='.$row['cat_id'].'">'.$row['cat_name'].'</option>';
}
echo '</select>';
?>

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 16 July 2006 - 10:40 PM

Thanks pixy.
I tried your code but there was no difference...  :-[


P.S. The PHP printf( ) function works much like the print( ) function in that it outputs (prints) a string (argument) to the web browser. However, you will use the printf( ) function when you want to control how this argument will be formatted.

see http://www.bellaonli...es/art30296.asp

using printf....

print ($row["cat_name"]);
print ( "</option>\n" );

becomes...

printf ($row["cat_name"], "</option>" );

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 16 July 2006 - 11:00 PM

Maybe you should take a screenshot of your database? Perhaps there's a problem with the values--since the PHP code looks valid to me.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 16 July 2006 - 11:30 PM

I'm sure it is not the DB because when I order the query, it skips either number 1 or 9.

#6 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 16 July 2006 - 11:36 PM

Try plugging the query directly into the database and see what it gives you:

SELECT * FROM `tbl_category` WHERE cat_parent_id=0;

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 July 2006 - 03:21 AM

The reason the OP was only getting 8 out of the 9 rows is that this line
<?php $row = mysql_fetch_array($result); ?>
is before the while loop. Remove that line and you should be ok.

BTW, instead of using 5 statements to output the line, use one:
<?php '<option value='.$row['cat_id'].'">'.$row['cat_name'].'</option>'; ?>
as pixy suggested.

Ken

#8 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 17 July 2006 - 04:06 AM

Thanks kenrbnsn!!

Somehow I didnt see that I had typed "$row = mysql_fetch_array($result)" twice.  ::)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users