Creating array using multiple columns from mysql results

I am new to PHP and have to create an array from a basic “select * from table” in mysql. What I am wanting to know is what is the proper way to create the array using specific columns returned in the record set. Would it be:




$array1 = array();


while(($x =  mysql_fetch_assoc($result))) {
    $array1[] = $x['column1'].$x['column2'].$x['column3'].$x['column4'];

}



Thanks.

what happened when you tried it?

I’m going to have to go with webdev … have you tried running the code example you gave?

What happened when you did?


$array1 = array();

while(($x =  mysql_fetch_assoc($result))) { 
    $array1[] = $x; 
}  

That’s a improvement on the above, as the while loop runs a new array will be appended to array1 (each of them arrays will have however many columns there are in the result set).

I’m not so sure.

The above code will generate a 1D array.

Personally, I would prefer to build a 2D array where each row in the array contains the data from a single row in $result.

This is something you could do in your select (if using Mysql anyhow).

[code=sql]
SELECT
CONCAT(column1, column2, column3, column4) as all_columns
FROM mytable



then your array is already in $x['allcolumns']


```php

$x =  mysql_fetch_assoc($result);
print_r($x['all_columns']); // array of columns concatenated together

You may be doing other things with the data, and this may not be a good fit. Just sayin’.

It shouldn’t… because fetch_assoc returns an ARRAY.

I think you’re completely misunderstanding php arrays and how mysql_ commands work… your first example makes a 1d array, because you’re stringing them together as… a string. Period is string addition, so you aren’t making an indexed array, just a flat list.

Returns an array… you add that array to an array, and it’s two dimensional… since php doesn’t have TRUE two dimensional arrays, only arrays OF array.

so the $array1=$x is adding the X array as another ‘row’ in $array1… which is as close to a two dimensional array as you’re going to get in PHP. (and really as close to an array of record as you’ll get either without needless bloat)

(fixed it :wink: )

OP could also look at stepping up to mysqli and using [FPHP]mysqli_fetch_all[/FPHP] instead.

Right about when I was editing the post to fix it…

Which is good advice – either mySQLi or PDO since so far as I’m concerned the mysql_ functions should be removed entirely for the next version of PHP… but what do I know, I say the same thing about <?php ?> and double quotes for strings.

Thought I had responded yesterday from smart phone but that wasn’t a smart way to do so :wink:

Here is an example of what worked:

$array1 = array();

while($x = mysql_fetch_assoc($result)) {
$array1[$x[“column1”]][$x[“column2”]][$x[“column3”]][$x[“column4”]];

}

My original code just was using “.” which of course just made it a concatenated string :slight_smile:

Appreciate the responses.

yep, I misread the code - multi-tasking is not my strong suit :lol: