RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

21 June 09

How to export to an array a MySQL query with 3 lines of code

By reading a good contribution on PHP site, I discovered this method for exporting to an array a query submitted to MySQL:
$result = mysql_query("SELECT * FROM table");
for($i = 0; $array[$i] = mysql_fetch_assoc($result); $i++) ;
array_pop($array); // removes last empty array
It produces:
Array
(
    [0] => Array
        (
            [id] => 1
            [user] => myuser
            [pass] => mypass
            ... other fields
        )
    ... and so on
That’s to say you’ll have count($array) records, of which every row has count($array[0]) fields.

Tags: mysql php SQL
Comments (View)
  1. andreaolivato reblogged this from codepuzzling
  2. codepuzzling posted this
blog comments powered by Disqus
Themed by Hunson. Originally by Josh