我想使用jQuery驱动的图表。如果jQuery需要这样的数据:

 data: [{
     period: '2011 Q1',
     sales: 1400,
     profit: 400
 }, {
     period: '2011 Q2',
     sales: 1100,
     profit: 600
 }, {
     period: '2011 Q3',
     sales: 1600,
     profit: 500
 }, {
     period: '2011 Q4',
     sales: 1200,
     profit: 400
 }, {
     period: '2012 Q1',
     sales: 1550,
     profit: 800
 }],


如何使用MySQL和PHP生成此文件?

最佳答案

此代码可以帮助您

$my_array = array();
$custom_variable = "anything";
$con=mysqli_connect("localhost",'username','password','databaseName');
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$fetch = mysqli_query($con,"SELECT * FROM your_table");

while ($row = mysqli_fetch_array($fetch)) {
    $my_array['col1'] = $row['col1'];
    $my_array['col2'] = $row['col2'];
    $my_array['custom_col'] = $custom_variable;
}

echo "data:".json_encode($my_array);
mysqli_close($con);

10-01 12:26
查看更多