Results 1 to 8 of 8

Thread: [RESOLVED] Whats wrong with my code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Resolved [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>

  2. #2
    Junior Member hairball's Avatar
    Join Date
    Aug 2006
    Location
    i'm not here.
    Posts
    24

    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>
    Last edited by hairball; Oct 16th, 2006 at 05:24 AM.

    Break out from your mental box.
    I found better company. Iced chocolate with coffee jelly, anyone?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Whats wrong with my code

    <option value="<? echo $result['cat_code']?>"><? echo $result['cat_desc'] ?></option>

  4. #4
    Junior Member hairball's Avatar
    Join Date
    Aug 2006
    Location
    i'm not here.
    Posts
    24

    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>

    Break out from your mental box.
    I found better company. Iced chocolate with coffee jelly, anyone?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Whats wrong with my code

    Stickler.

  6. #6
    Junior Member hairball's Avatar
    Join Date
    Aug 2006
    Location
    i'm not here.
    Posts
    24

    Re: Whats wrong with my code

    Quote Originally Posted by mendhak
    Stickler.
    Likewise.

    Break out from your mental box.
    I found better company. Iced chocolate with coffee jelly, anyone?

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    30

    Re: Whats wrong with my code

    Thanks a lot everybody..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width