Home > DeveloperSection > Forums > Getting SQL result into an array
Garry Winogrand
Garry Winogrand

Total Post:24

Points:168
Posted on    July-08-2013 8:44 AM

 JSON JSON 
Ratings:


 1 Reply(s)
 873  View(s)
Rate this:
Hi Developers!

I'm trying to get a MySQL result like "SELECT * FROM sales" into an array that looks like this:

$data = array( 
         array('Date', 'Sales'),  
         array('June 25', 12.25),  
         array('June 26', 8.00) 
 );

I tried it with a while loop but it didn't work. How can i do this?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    July-08-2013 8:58 AM

Hi Garry!

Try this

$query = "SELECT * FROM sales";
$data = array();
$data[] =array('Date', 'Sales');

if ($result = $mysqli->query($query)) 
{        
    while ($line = $result->fetch_assoc()) 
    {        
        // line contain an associative array with "Date" and "Sales"  
        $data[] = array($line['Date'], $line['Sales']);
    }
}

$result->free();

I hope it helpful for you!

Don't want to miss updates? Please click the below button!

Follow MindStick