Jump to content

Missing SQL row


laide234

Recommended Posts

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??? ???
Link to comment
https://forums.phpfreaks.com/topic/14798-missing-sql-row/
Share on other sites

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>';
?>
Link to comment
https://forums.phpfreaks.com/topic/14798-missing-sql-row/#findComment-59096
Share on other sites

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 [url=http://www.bellaonline.com/articles/art30296.asp]http://www.bellaonline.com/articles/art30296.asp[/url]

using printf....

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

becomes...

printf ($row["cat_name"], "</option>" );
Link to comment
https://forums.phpfreaks.com/topic/14798-missing-sql-row/#findComment-59098
Share on other sites

The reason the OP was only getting 8 out of the 9 rows is that this line
[code]<?php $row = mysql_fetch_array($result); ?>[/code]
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:
[code]<?php '<option value='.$row['cat_id'].'">'.$row['cat_name'].'</option>'; ?>[/code]
as pixy suggested.

Ken
Link to comment
https://forums.phpfreaks.com/topic/14798-missing-sql-row/#findComment-59143
Share on other sites

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.