Php 使用JSON的数据库连接
我的php代码Php 使用JSON的数据库连接,php,sql,json,sql-server,Php,Sql,Json,Sql Server,我的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 = sqlsr
<?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文件中获取JSON格式的数据。
从PHP文件接收的JSON:
var obj、dbParam、xmlhttp;
obj={“表”:“PointEvent”,“limit”:10};
dbParam=JSON.stringify(obj);
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“demo”).innerHTML=this.responseText;
}
};
open(“GET”、“testsql.php?x=“+dbParam,true”);
xmlhttp.send();
嗨
上面是我的PHP和HTML代码,我尝试使用Json(编码和解码方法)连接sql数据库,并尝试打印单列,但得到的错误是
'警告:sqlsrv_fetch_array()要求参数1是资源,布尔值在'
有谁能帮我做这件事吗
提前谢谢 我在您的查询“添加空间”中发现了连接问题,否则查询看起来像“从mytableLIMIT1中选择点” 从 到 通过在while之前添加行,也可以正确处理异常
if( $stmt === false ) { /*!Handle exception here*/ }
看来你对编程还不熟悉。在使用数据库时,千万不要信任原始用户输入,如$\u GET[“x”]。进行适当的清理,否则将导致sql注入
请通过以下链接了解更多信息
带有
$stmt=sqlsrv\u查询($conn,$sql)的行代码>语句可能会导致您的问题。我认为$sql
格式不好。我不确定MS SQL Server是否有'LIMIT'
关键字,因此请先尝试以下内容:$SQL=“从“$obj->table”中选择点代码>
在使用sqlsrv\u fetch\u array()之前,还必须始终检查sqlsrv\u query()
结果:
我在查询中使用了空格,但没有得到确切的输出。我的输出类似于数组([0]=>Array([0]=>42000[SQLSTATE]=>42000[1]=>102[code]=>102[2]=>[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]靠近“10”的语法不正确。[消息]=>[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]靠近“10”的语法不正确。))我认为这可能是转义内容的问题。您是否在查询变量内容中使用了$,可能是表名?sry我没有得到u。你能在这里写下查询吗?如何使用查询变量内容中的$,可能是表名…请问变量$obj->table中有字符串“$”吗?例如,“mydatabase$mytablename”例如$a=“stack”$b=“溢出”;回声“$a$b”//堆栈溢出。回声“$a\$b”//堆叠$b
$sql = "SELECT Point FROM ".$obj->table." LIMIT ".$obj->limit;
if( $stmt === false ) { /*!Handle exception here*/ }
$sql = "SELECT Point FROM ".$obj->table;
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
...