Results 1 to 2 of 2
  1. #1
    Join Date
    May 2003

    Unanswered: Fetching data multiple times


    In a PHP script, I need the data fetched from a table at multiple places to display it. I am fetching the data as below:

    $qry2 = "SELECT ownerid, ownername FROM owners";
    $rslt2 = mysql_query ($qry2, $db);
    while ($row2 = mysql_fetch_array($result2, MYSQL_BOTH))
    echo $row2["ownerid"].'='.$row2["ownername"];

    How should I code this to avoid the fetch from the table those many times I need the data? Can I read all these results into an array?

    Thanks in advance!

  2. #2
    Join Date
    Oct 2003
    You should read into the PEAR DB Libraries.
    Make sure you checkout the fetchmode assoc section....

    In reference to your question I do this with a $counter in the while loop.
    NOTE: fetchmode is equal to mysql_fetch_array

    $sql = "SELECT * FROM BLAH";

    $result = $db->query($sql);
    $counter = 0;
    while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
    $search_results[$counter] = $row;

    and then to display
    foreach ($search_results as $k => $v){
    foreach ($v as $a => $b){
    echo "<br>$k === $a => $b";

    This would display
    0 === fname => george
    0 === lname => bush
    1 === fname => joe
    1 === lname => mama
    etc etc...

    Hope this helps...
    -Jarrod Lash

Posting Permissions

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