【发布时间】:2018-06-01 07:01:11
【问题描述】:
我的php代码,
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_GET["x"], false);
$serverName = "AE58RETY245YU";
$connectionInfo = array( "Database"=>"Test", "UID"=>"bala",
"PWD"=>"bala");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$outp=array();
$sql = "SELECT Point FROM ".$obj->table."LIMIT".$obj->limit;
$stmt = sqlsrv_query( $conn, $sql );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$outp=$row['Point'];
}
echo json_encode($outp);
?>
我的html代码,
<!DOCTYPE html>
<html>
<body>
<h2>Get data as JSON from a PHP file on the server.</h2>
<p>The JSON received from the PHP file:</p>
<p id="demo"></p>
<script>
var obj, dbParam, xmlhttp;
obj = { "table":"PointEvent", "limit":10 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "testsql.php?x=" + dbParam, true);
xmlhttp.send();
</script>
</body>
</html>
你好,
以上是我的 PHP 和 HTML 代码,我尝试使用 Json(编码和解码方法)连接 sql 数据库并尝试打印单列,但得到的错误是
'警告:sqlsrv_fetch_array() 期望参数 1 是资源,布尔值在'中给出。
谁能帮我解决这个问题。
提前致谢。
【问题讨论】:
标签: php sql json sql-server