Results 1 to 6 of 6

Thread: PHP & mysql array

  1. #1
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default PHP & mysql array

    Hi,

    I want to display some field from db and use the followings - learnt from some tutorial - to display with format:
    Code:
    <?php 
    require ('config.php');
    $query="SELECT * FROM movie";
    $result=mysql_query($query,$connection);
    while($row = mysql_fetch_array($result)){
    printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);} ?>
    Right now, the code print all the rows from the table 'movie'.
    How do I make it to print only say the first row?

    Thanks.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    eh, here's what I'd do. I don't use printf, nor mysql_fetch_array, so not sure what's "wrong" with those...but... here:

    PHP Code:
    <?php 
    require ('config.php');
    $query="SELECT * FROM movie";
    $result=mysql_query($query,$connection);
    while(
    $row mysql_fetch_assoc($result)){
       echo 
    "<b>Title:</b> %s<br> <b>Origin:</b> %s<br>".$row["title"].$row["origin"]);
       }
    ?>

    assoc vs. array--- you can configure array to be assoc, but why, plus, you didn't.
    basically array is numbers ($row[1]) vs assoc is names ($row['name']).

    echo.... works for me. why are you using print? I have no clue why there are two commands, so you may be totally right.
    however, echo will work as is right now.

  3. #3
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow

    Thanks.
    Clarify:

    What I meant by first row is 'first recordset' -- maybe this is the correct word.
    1st record set will be e.g:
    Title: Mystic River
    Origin: America
    2nd record set:
    Title: Fearless
    Origin: Hong Kong

    What if I only want to print/ echo one recordset.

    Reason: I have lots of thumbnails of movies, and use ToolTip with mouseover. I want: when a thumbnail is mouseover, the ToolTip will use info from db, but only 1 recordset that's relevant to the thumbnail.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Rather than:
    Code:
    while($row = mysql_fetch_array($result)){
    printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);} ?>
    Use:
    Code:
      $row = mysql_fetch_array($result);
      printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);
    ?>
    That's some pretty ugly code. Keeping it neat will save you headaches later on.
    Both mysql_fetch_assoc() and mysql_fetch_row(), the former returning an associative array and the latter returning a numerical array, are pointless aliases, of which PHP has many. They're not technically deprecated in favour of mysql_fetch_array(), but they should be. mysql_fetch_array() can take a second argument: MYSQL_NUM to behave like mysql_fetch_row(), MYSQL_ASSOC to behave like mysql_fetch_assoc(), and MYSQL_BOTH to fetch an array with both numerical and associative indices, which is the default behaviour.
    Last edited by Twey; 05-01-2006 at 12:28 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mtran
    How do I make it to print only say the first row?
    There are two options, here.

    Add a LIMIT clause to the query so that the query itself only returns a single tuple. This has the advantage of speeding up the query itself, but it's clearly not appropriate if you want the other results for some other purpose.

    PHP Code:
    $query "SELECT * FROM movie LIMIT 1"
    However, you should also add an ORDER BY clause, too, to ensure that the returned value is predictable.

    The second option is to remove the while loop (as Twey demonstrated).

    You'll have to decide which is best under your circumstances.

    What I meant by first row is 'first recordset' -- maybe this is the correct word.
    No, it isn't. The word 'recordset' is a synonym for query result. The word you want is record or tuple.

    Mike
    Last edited by mwinter; 05-01-2006 at 12:32 PM.

  6. #6
    Join Date
    Nov 2005
    Location
    Austin TX,US
    Posts
    71
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I figured it out. Thanks a lot for your help!

    -----------------
    THANKS!
    It sounds silly [and probably it's !] to ask, but I don't know yet how to use that code, either with LIMIT 1 or without WHILE, to display the SECOND record only, and the THIRD record only,...b/c I have other movie thumbnails also.

    Or maybe you can help me with a better solution for what I'm doing: I'm doing something like this but much much simpler of course: http://www.netflix.com/BrowseSelection?lnkctr=nmhbs

    Only the info for the tooltip [Title, origin, year,...] is from a db -- all others such as image, title on the page are hardcoded.
    Thank you! Really appreciate your help!
    Last edited by mtran; 05-01-2006 at 08:48 PM.

Bookmarks

Posting Permissions

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