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:
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).
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)
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.