using a query twice?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

using a query twice?

Post by s.dot »

Is something like this possible?

Code: Select all

$result = mysql_query("SELECT * FROM table");

// use 1
while($array = mysql_fetch_assoc($result))
{
   // do this
}

// use 2
while($array2 = mysql_fetch_assoc($result))
{
   // do this
}
Currently it's not working in my script.. is there a way to make it work?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_data_seek()

however, it's recommended (memory allowing) that you cache the original fetches into an array. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

so if I'd want to return the internal datapointer to the first row (as if I just made the query) I'd use mysql_data_seek($result,0) ?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Following from feyd's comments, I use a function similar to the following for storing results in an array:

Code: Select all

<?php
function getResult ($query, $data = array()) {

    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result)) {

        $data[] = $row;

    }

    return $data;

}
?>
Then you can use $data to index both the rows and the columns, for example $data[0][0] will provide the value of the 1st row, 1st column :)


EDIT: And to your question above, yes.
Last edited by Jenk on Sun Oct 02, 2005 10:03 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

okay.. that worked. I assume it's correct. guess i'll figure it out when i complete the script

thanks, feyd.. as always :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply