我想像这样输出
var data = {
{
"cname": "Albania"
}
{
"cname": "Austria"
}
}
我试过这段代码,但收到错误"Parse error: syntax error, unexpected T_VAR in C:\wamp\www\mvc\map\data.php on line 22"
<?php
header('Content-type: application/json'); // this is the magic that sets responseJSON
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
// Connecting, selecting database
$link = mysql_connect($dbhost, $dbuser, $dbpass)
or die('Could not connect: ' . mysql_error());
mysql_select_db($dbname) or die('Could not select database');
$query = "SELECT cname FROM country"; // Performing SQL query
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$all_recs = array();
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$all_recs[] = $line;
}
var data = {
echo json_encode($all_recs);
};
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
任何人都可以指导我如何解决此问题,谢谢
答案 0 :(得分:5)
我不确定你为什么要这样,但是你可以在php中混合使用js代码,你可以试试
$jsonData = json_encode($all_recs);
echo "var data = {$jsonData}";
答案 1 :(得分:0)
$data = "cname : Albania cname : Austria";