Forum Moderators: coopster

Message Too Old, No Replies

Put mysql query into an array

Put mysql query into an array

         

snowman304

8:57 am on Nov 11, 2007 (gmt 0)

10+ Year Member



I am having trouble putting a mysql query into an array. I want the total result to be an array. So far, I found out I can use mysql_fetch_array to get one row, but how do I combine all the rows into 1 array?

I seen a lot of examples on using a while statement to print each row, but that is not what I am needing to do. I need to have the total result as one array that I use.

I am in PHP4

Thanks,
Mike

[EDIT] - After thinking about this some, let me describe in detail what I am trying to do.

I have a DB that has a list of fish that was caught, that was entered by a user. I do a select to get a mysql result of everything put into the species table. I then need to count how many times each species was entered, which I think I can use array_count_values. I need to end up with a result like this:

$fishcount = array ( array ( "", "Total Logged"),
array ( "Fish 1 name", 5),
array ( "Fish 1 name", 2),
array ( "Fish 1 name", 6),
array ( "Fish 1 name", 8)
);

Habtom

10:23 am on Nov 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A while loop on Mysql_fetch_array will get you all the values into an array.

See an example at mysql_fetch_array [php.net]

snowman304

3:42 pm on Nov 11, 2007 (gmt 0)

10+ Year Member



That got me on the right track. I could have swore that I looked at that page before. Must have missed some of the comments.

Anyways..........Using mysql_fetch_array with MYSQL_NUM, puts me with this:

Array ( [0] => Array ( [0] => Fish 1 ) 
[1] => Array ( [0] => Fish 2 )
[2] => Array ( [0] => Fish 3 )
[3] => Array ( [0] => Fish 2 )
[4] => Array ( [0] => Fish 3 )
)

Now the problems comes in, I can't seem to use array_count_values or array_values. array_count_values throws an error saying it only counts strings and int. And array_values just displays it the same, like I posted above.

snowman304

4:26 pm on Nov 11, 2007 (gmt 0)

10+ Year Member



I was able to flaten the multi-dim. array so it listed only values. Then I used the array_count_values and ended up with this:

Array ( [Fish 1] => 1 [Fish 2] => 2 [Fish 3] => 2 )

Ok, I can work with this. I think. lol Anyways, at least I am getting somewhere. lol

Thanks,
Mike