我有1个数据库。我有一个表格。
我在这段代码中做json解析。
我使用这个PHP代码来检索值:
<head>
<style type="text/css">
.auto-style1 {
text-align: right;
}
</style>
</head>
<?php
echo getdata($_REQUEST['lastupdate']);
function getdata($lastupdatedate){
$obj = json_decode($json);
$con = mysql_connect("localhost","root","Password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("roster", $con);
$query = "select * from statedeathtax_v2 where LastUpdated > '".$lastupdatedate."' order by LastUpdated";
$rs = mysql_query($query) or die($query);
//print_r($rs);
while($row=mysql_fetch_assoc($rs)){
$record[] = $row;
}
$data = json_encode($record);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
return $data;
}
?>
此处此代码不显示像“$”这样的特殊字符。 当我在此代码中抛出查询时,它会显示具有特殊字符的列的空值。 我没有得到这个问题。
答案 0 :(得分:0)
我已经解决了问题:
更新代码:
<?php
echo getdata($_REQUEST['lastupdate']);
function getdata($lastupdatedate){
$obj = json_decode($json);
$con = mysql_connect("localhost","root","Password@123");
mysql_set_charset('utf8',$con);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("roster", $con);
$query = "select * from statedeathtax_v2 where LastUpdated > '".$lastupdatedate."' order by LastUpdated";
$rs = mysql_query($query) or die($query);
//print_r($rs);
while($row=mysql_fetch_assoc($rs)){
$record[] = $row;
}
$data = json_encode($record);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Type: application/json; charset=utf-8' );
return $data;
}
?>
答案 1 :(得分:0)
对于mysqli_connect:
$conecta = mysqli_connect("host", "user", "pass", "dbase")
or print (mysql_error());
$conecta->set_charset('utf8');
查看更多信息 http://dev.mysql.com/doc/apis-php/en/apis-php-mysqlinfo.concepts.charset.html