Jump to content

putting multiple result form one query into variables/array


hgmme@wa

Recommended Posts

(You might want to read this post for the topic that this stemmed from. (it's on the topic of sql)I need to know how to put results from the two different rows and the individual columns into variables/arrays. Should I do something like this:

<?php$query = mysql_query("SELECT share_info, share_email, share_website FROM wp_UserAllow WHERE viewed_id=0 AND reviewers_id=0UNIONSELECT * FROM wp_UserAllow WHERE viewed_id=1 AND reviewers_id=1ORDER BY viewed_id ASC") or die("Failed: " . mysql_error());while ( $rows = mysql_fetch_assoc( $query )) {	$num = 1;	$results = array 	(		$num=>array		(			"share_info"=>$rows['share_info'],			"share_email"=>$rows['share_email'],			"share_website"=>$rows['share_website']		)	)	$num++}echo "and now I can just echo " . $results['1']['share_info'] . " or " . $results['2']['share_email'] . " or whatever I want to do with the different array parts???"?>

I'll actually be using the stuff in "if" statements, but hopefully you get the idea of what I need... So, will it work?

Link to comment
Share on other sites

you could do this

 $sql= mysql_query("SELECT * FROM table ORDER BY id DESC");while($result= mysql_fetch_array($sql)){echo $result['share_email'];echo $result['share_info'];echo $result['share_website'];//IF YOU SPECIFIED WHAT YOU WANT TO SELECT YOU DO THIS EG $sql= mysql_query("SELECT share_email,share_info,share_website FROM table ORDER BY id DESC");while($result= mysql_fetch_array($sql)){echo $result['1'];echo $result['2'];echo $result['3'];}

Link to comment
Share on other sites

hgmme: you have the right idea, but not carried out properly. I'll describe my fixmc_keny: while that does output all of his variables, it doesn't put it into an array to be fetched later.

$num=1; //this needs to be outside the loop$results = array(); //lets just make sure we start off with an empty arraywhile ( $rows = mysql_fetch_assoc( $query )) {	$results[$num] = array		(			"share_info"=>$rows['share_info'],			"share_email"=>$rows['share_email'],			"share_website"=>$rows['share_website']		)	)	$num++}print_r($results); //Here you can see that $results has all of your information

Link to comment
Share on other sites

each time it goes through the loop it will set $n=1 again. Despite the fact that you have $n++ at the end of the loop (making $n=2), when it goes back to the top, it will be set to 1.Your $result array will then only have 1 element that contains the very last row from the mysql query.

Link to comment
Share on other sites

no problem, and seeing what I wrote again this morning, I remember a faster way of doing this. The way you were doing earlier is fine if you need do a transform on any of the fields. But if you want all the fields and keep the exact same field names this way is faster:

$query = mysql_query("SELECT * FROM map_data WHERE 1 LIMIT 5");$num=1; //this needs to be outside the loop$results = array(); //lets just make sure we start off with an empty arraywhile ( $results[$num] = mysql_fetch_assoc( $query )) {	$num++;}print_r($results);

In the previous code, $row = mysql_fetch_assos($query) gives us $row that already has the elements we wanted to assign into $results[], so reassigning again is an unnecessary extra step...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...