MySQL fetch_assoc question
Is it possible to use a nested mysql_fetch_assoc
Basically I used the fi开发者_如何学Gorst one to populate text boxes, now from another table I need to fetch and image. How do I go about doing this without closing the first one, because I have more text boxes below this image that need to be fetched.
while($row = mysql_fetch_assoc($result)){
?>
<form id="myform" name="myform" action="profiledo.php" method="post">
<p>First Name
<input type="text" name="firstname" id="textfield" value="<?php echo( htmlspecialchars( $row['FirstName'] ) ); ?>" />
<br />
in order to be able to fetch $result more times (I see no reason though), you'll have to make single query for each fetch. I see no reason for doing so though
I think you are looking for a single query with a join. Something like:
SELECT * FROM `Profile` NATURAL JOIN `Table with Image`
But the exact query depends on the schemas of your two tables.
I think that it is more beneficial to put all of the information you want quickly into an array and accessing the retrieved data from memory, as opposed to slowly adding it while the query is looping through it.
// Read records
$query = "SELECT * FROM table WHERE condition";
$query = mysql_query($query);
// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;
// Delete last empty one
array_pop($array);
You can use print_r($array) to see the results.
If the image is not on the same table, you could join the tables via a similar identifier (unique user id?).
$query = "SELECT * FROM table1, table2 WHERE table1.id=table2.id";
Then the first user's First name would be: $array[0]['FirstName'], second would be $array[1]['FirstName'], etc.
Blake
The answer is, yes it is possible to use nested mysql_fetch_assoc().
Though I'm not very sure on the question and what does your current implementation looks like but here are some suggestion anyways:
Use a JOIN query: though it is possible only if the data contained in your text-boxes is related to the image. From what little information you have provided, I guess the image is the user's image and is stored in a separate table. If that is true, then the most obvious implementation is that the image table is referenced by some user id. Assuming that there is a
User
andUserImage
tables, this is how the JOIN query might look like:SELECT `User.*`, `UserImage`.`image` FROM `User` INNER JOIN `UserImage` ON `User`.`id` = `UserImage`.`user_id` WHERE `User`.`id` = xxx;
Please note that the above is only a template query and you'll need to change this as per your schema and logic. Also, I missed to mention that the above implementation might need some extra logic if there can be multiple images per user.
A nested mysql_fetch_assoc() is possible but it depends on your implementation. As you've mentioned that the first (or outer) mysql_fetch_assoc() is used to fill-in text boxes. And within this, you would like to run another mysql_fetch_assoc() to fetch the image. So, if the image query is run on a separate table and is run as a separate query, then you might store its result in a separate variable, say $result1 and then run the inner mysql_fetch_assoc() on that resultset.
while ($row = mysql_fetch_assoc($result)) { // $row array now contains the profile details $result1 = mysql_query('the query for fetching the image'); while ($row1 = mysql_fetch_assoc($result1)) { // $row1 array now contains the image } }
It is important that the inner mysql_fetch_assoc() runs on a separate resultset.
Hope the above helps! If it is none of the above that you are trying to do, I'd appreciate if you could provide more inputs on what you require.
精彩评论