Re: querying results already queryed once before

From: Date: Tue, 15 Jan 2002 20:03:20 +0000
Subject: Re: querying results already queryed once before
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-80617@lists.php.net to get a copy of this message
On Tuesday, January 15, 2002, at 02:18 PM, Phil Schwarzmann wrote:
Dumb question: How do I query results from a MySQL that I've already queried once before in the same page?
Look, buddy, *I* ask the dumb questions around here!
Here is my code: // querying the database for the first time. $query = "select * from table where thingA = $searchA"; $result = mysql_query ($query); // now I want to query my results I just made.
Here's how I do it: First of all, don't forget the second argument to mysql_query(). It should be a variable that contains your database connection info (see mysql_connect() in the function list). Your variable "$result" is now a container for the query you just made. The results of the query are returned as an array stored inside this container ("$result"). So you need to use any of the functions which act on that array -- they typically begin with "mysql_fetch_". mysql_fetch_row() -- you can use this function to grab results based on their numeric index. mysql_fetch_assoc() -- you can use this function to grab results based on their associative index. mysql_fetch_array() -- you can use this function to grab results using either the numeric or the associative index. This one's my favorite, since it's flexible like that. So how do you do it? Here's what I do: $query = "SELECT * FROM table WHERE thingA='$searchA'"; $result = mysql_query($query, $db); while ($row = mysql_fetch_array($result)) { $thingA = $row['thingA']; // the associative index is the column name
     print $thingA;
} Erik

« previous php.general (#80617) next »