将SQL结果导入数组

时间:2013-07-08 14:09:23

标签: php mysql sql arrays json

我正在尝试将类似“SELECT * FROM sales”的MySQL结果转换为如下所示的数组:

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

我用while循环尝试了它,但它没有用。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

试试这个:

$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();