Welcome to the OzoneAsylum FaqWiki
Frequently Asked Questions
Server Side Coding
PHP

How do I dump all the contents of a database table into an array? Pages that link to <a href="https://ozoneasylum.com/backlink?for=5779" title="Pages that link to How do I dump all the contents of a database table into an array?" rel="nofollow" >How do I dump all the contents of a database table into an array?\

Bitdamaged has provided a neat bit of code to create a multi-dimensional array holding all the data from a mySQL query:

code:
$result = mysql_query("SELECT * FROM table");
$multi_array = array();
while($row = mysql_fetch_row($result)) {
array_push($multi_array, mysql_fetch_array($row));
}



Tyberius Prime:
which is the equivalent to

code:
$sresult = mysql_query("SELET * FROM table");
$multi_array = array();
while ($row = mysql_fetch_array($result))
$multi_array&#91;&#93; = $row;
}


which has the bennefit of you being able to say something like $multi_array[ 3 ][ 'fieldname' ].

But you should be aware that php usually can hold only about 8 megs of data in memory. So if you got a bigger database, you can't read it all into an array. there are few cases where you should do that anyway, as well.


-------------------------
Relevant threads:

MySQL query for EVERYthing

___________________
Emperor

(Added by: Emperor on Wed 06-Aug-2003)

(Edited by: Tyberius Prime on Thu 07-Aug-2003)

(Edited by: Tyberius Prime on Thu 07-Aug-2003)

« BackwardsOnwards »

Show Forum Drop Down Menu