[RESOLVED] Whats wrong with my code
Hi all..i dont know what's wrong with my codes below..i have one table named category which have cat_id,cat_code,cat_desc.I want to list out cat_code.It success.All data will listed out but in blank.Thanks in advance
PHP Code:
<select name="cat_code">
<?
$sqlstmt=mysql_query(" SELECT cat_code FROM category");
while($result=mysql_fetch_array($sqlstmt))
{?>
<option value="<? echo $result['cat_code']?>"></option>
<? } ?>
</select>
Re: Whats wrong with my code
Your option has no caption or text, so it is displayed blank.
PHP Code:
<option value="<? echo $result['cat_code']?>">PUT CAPTION HERE</option>
Re: Whats wrong with my code
<option value="<? echo $result['cat_code']?>"><? echo $result['cat_desc'] ?></option>
Re: Whats wrong with my code
Looks better. :)
PHP Code:
<select name="cat_code">
<?
$sqlstmt=mysql_query(" SELECT cat_code, cat_desc FROM category");
while($result=mysql_fetch_array($sqlstmt))
{?>
<option value="<? echo $result['cat_code']?>"><? echo $result['cat_desc'] ?></option>
<? } ?>
</select>
Re: Whats wrong with my code
Re: Whats wrong with my code
Quote:
Originally Posted by mendhak
Stickler.
Likewise.
Re: Whats wrong with my code
Quote:
Originally Posted by hairball
Looks better. :)
PHP Code:
<select name="cat_code">
<?
$sqlstmt=mysql_query(" SELECT cat_code, cat_desc FROM category");
while($result=mysql_fetch_array($sqlstmt))
{?>
<option value="<? echo $result['cat_code']?>"><? echo $result['cat_desc'] ?></option>
<? } ?>
</select>
Never use the short tags <? and <?=. These are not supported on many servers, and most likely will not be forwards compatible with PHP 6.
If you only want to reference rows by name, mysql_fetch_assoc() is more efficient.
Also, with constructs spanning HTML sections, you can use the alternative syntax, like this:
PHP Code:
<select name="cat_code">
<?php
$result_set = mysql_query("SELECT cat_code, cat_desc FROM category");
while ($row = mysql_fetch_assoc($result_set)):
?>
<option value="<?php echo $row['cat_code'] ?>"><?php echo $row['cat_desc'] ?></option>
<?php endwhile; ?>
</select>
Finally, I do recommend using a better data access API, like PDO, etc., if you can.
Re: Whats wrong with my code
Thanks a lot everybody.. :)