Jump to content

mysql result into an array


hyster

Recommended Posts

im trying to get a MySQL result to be stored in a variable(array) so I can use the array later.

ive never had to do this before and the search's ive done don't seem to do what I need.

 

the end result im after is a members list. the array im tryin to build will be compared to another query and the other query will have the names removed so I don't echo the same name twice. my code works with a manual array but I want to build the array from a different table hence this query I carnt get working. 

$sql5 = "SELECT * FROM officer order by 'name' asc";
$result5=mysql_query($sql5) or die ( mysql_error () );

while($row5 = mysql_fetch_array( $result5 )) {

$test = array($row5['name']);

}
//
//the end result I want from the above query so I can use it later
$test = array("name1", "name2", "name3", "name4", "name5", "name6");
Link to comment
https://forums.phpfreaks.com/topic/278031-mysql-result-into-an-array/
Share on other sites

Do you have error checking turned on?  I'd be surprised if line 6 doesn't throw a warning or something.

 

Anyway - line 6 is trying to typecast a simple variable as an array and assign it (as an array?) to a var called $test.  But then the next iteration is going to do the exact same thing with the next row, so basically you'll end up with the last row's value in the var $test. 

 

Perhaps you should turn on error checking when you are developing.  And as an answer to your woes, try doing this:

 

$test[] = $row5['name'];

 

This will give you an array have the contents of each 'name' field in it.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.