Find the answer to your Linux question:
Results 1 to 2 of 2
Wondering how to loop a query result in a FOR loop like so... (doesn't work obviously)... $result = mysql_fetch_assoc($qry); for ($i=0; $i<mysql_num_rows($result);$i++){ echo $result['column'][$i] . $result['column2'][$i]; // for row each ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    d0p
    d0p is offline
    Just Joined!
    Join Date
    Jul 2005
    Posts
    60

    PHP howdoi loop a query in for(..)


    Wondering how to loop a query result in a FOR loop like so... (doesn't work obviously)...

    $result = mysql_fetch_assoc($qry);

    for ($i=0; $i<mysql_num_rows($result);$i++){
    echo $result['column'][$i] . $result['column2'][$i]; // for row each row
    }

  2. #2
    Linux User
    Join Date
    Dec 2004
    Posts
    323
    The short answer to this issue is that you need to call a class which handles database actions. Have a look at http://pear.php.net/packages.php?cat...tname=Database and download what you think is suitable. I am assuming that you will not program this in isolation and you will definitely benefit from abstraction layers such as from PEAR.

    The long answer is that you need to say print_r($result) on line 2, ie before the for loop to see what your data looks like. It may print something like this:
    PHP Code:
    Array(0=>Array(id=>100name=>'ABC'), 1=>Array(id=>200name=>'DEF'), ...) 
    Then instead of a for you are better off using a foreach, such that the result will look something like this:
    PHP Code:
    foreach($result as $r) echo $r['id'] . "," $r['name'] . "\n"
    (or something similar, I haven't actally tested this).
    The most important step is the first one: you have to know exactly what your result looks like before you can read out its constituent data in the format you want to.

    Hope this helps

    tech

Posting Permissions

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