如何在javascript中用PHP调用SQL记录[关闭]

<?php include('db.php'); 

//it's connect the database to server

$sql = "select * from event";
$query = mysql_query ($sql);
?>


<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2016-03-01',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: [
        <?php while($rs = mysql_fetch_array ($query)){
        ?>
            {
                title: '<?php echo $rs['title']?>',
                start: '<?php echo $rs['date']?>'
            },
        <?php }?>
        ]
    });
});

I want to show title and date as long as there is record in my database table. But this code just doesn't work. Thanks

Try this

<?php include('db.php'); 

    //it's connect the database to server

$sql = "select * from event";
$query = mysql_query ($sql);
$events = array();
while($rs = mysql_fetch_array ($query)){
    $events[] = array(
        'title' => $rs['title'],
        'start' => date('Y-m-d H:i:s', strtotime($rs['date']))
    );
}
?>


<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        defaultDate: '2016-03-01',
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        events: <?php echo json_encode($events)?>
    });
});