Jump to content

mysql result into an array


hyster
Go to solution Solved by 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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.