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