PHP生成jQuery数据

I want to use a jQuery powered chart. If the jQuery wants data like such:

 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
 }],

How do I generate this using MySQL and PHP?

This code may helps you

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

Largely this will depend on how you have the data stored. Easy way is to put your data into an Array and present it as a String using json_encode().

<script>
var myData = <?php echo json_encode($myArray); ?>;
</script>

Sample code to fetch data from a table and made it as JSON..

 $var = array();
 $result = mysqli_query($con, "SELECT * FROM TableName");
 while($obj = mysqli_fetch_object($result)) {
    $var[] = $obj;
 }
 echo 'users:'.json_encode($var).'}';

HERE is the refence.. and you can see the structure of JSON HERE.