[英]return Json array from PHP mySQL - Error
这是我的PHP代码,我想获取与$ _GET(“ ProductGroup”)变量相关的JSON数组。 但是,当我通过URL http://iilsfa.br0s.info/SFA/get_all_products.php?%27Laptops%27提交参数登录页面时,将显示此错误。
您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法
我找不到错误在哪里。 请帮忙
<?php
include_once './db_connect.php';
function getProducts(){
$db = new DB_CONNECT();
// array for json response
$response = array();
$response["products"] = array();
// Mysql select query
// check for post data
if (isset($_GET["ProductGroup"])) {
$selected_group = $_GET['ProductGroup'];
}
$result = mysql_query("SELECT * FROM Product WHERE ProductType= $selected_group")or die(mysql_error());
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["ProductID"];
$tmp["name"] = $row["ProductName"];
$tmp["type"] = $row["ProductType"];
$tmp["image"] = $row["ProductImage"];
$tmp["des"] = $row["ProductDescription"];
// push category to final json array
array_push($response["products"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($response);
}
getProducts();
?>
PS-我已经将我的SQL查询更改为此
$result = mysql_query("SELECT * FROM Product WHERE ProductType= '".mysql_real_escape_string('$selected_group')."'")or die(mysql_error());
现在它不显示先前的错误,而是显示一个空的json字符串。 我已经用phpmyAdmin.it检查了SQL查询是否正常工作并产生结果。
引用参数...
$result = mysql_query("SELECT * FROM Product WHERE ProductType= '$selected_group'")or die(mysql_error());
您不得使用mysql_ * 。
还要看看mysqli_real_escape_string
请在下面尝试。 不要使用简单的查询。 请参见SQL注入。 SQL注入使用mysql_real_escape_string防止SQL注入
<?php
include_once './db_connect.php';
function getProducts(){
$db = new DB_CONNECT();
// array for json response
$response = array();
$response["products"] = array();
// Mysql select query
// check for post data
if (isset($_GET["ProductGroup"])) {
$selected_group = $_GET['ProductGroup'];
}
$result = mysql_query("SELECT * FROM Product WHERE ProductType= '".mysql_real_escape_string($selected_group)."'")or die(mysql_error());
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["ProductID"];
$tmp["name"] = $row["ProductName"];
$tmp["type"] = $row["ProductType"];
$tmp["image"] = $row["ProductImage"];
$tmp["des"] = $row["ProductDescription"];
// push category to final json array
array_push($response["products"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($response);
}
getProducts();
?>
首先,即使对于初学者来说,您的代码也有充分的文档记录,仅在需要的地方记录的太多。
现在,您的错误在于查询本身。
$result = mysql_query("SELECT * FROM Product WHERE ProductType= $selected_group")or die(mysql_error());
如果查询中的字段为字符串类型,则必须在其周围添加单引号,否则解释器会认为您正在寻找列名。 下面是一个解决方案。
$result = mysql_query("SELECT * FROM Product WHERE ProductType= '$selected_group'")or die(mysql_error());
但我要补充一点,您的查询不安全。 它允许SQL注入,这可能会损害您的数据库数据。 有关SQLi的更多信息,请参考此页
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.