The information can be queried on the PHP page, but undefined is displayed on the front-end page
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
<p>
<input type="text" name="idNum" id="idNum">
<input type="button" value="查询" id="btnSearch">
<ul></ul>
</p>
<p id="myp"></p>
<script type="text/javascript">
$(function(){
$("#btnSearch").bind("click",function(){
//ajax
$.post(
"search.php",
{num:$("#idNum").val()},
function (data) {
console.log(data);
$("ul").append(
"<li>mac:"+data.mac+"</li>"+"<li>name:"+data.name+"</li>"+"<li>password:"+data.password+"</li>"
);
}
);
})
});
</script>
</body>
</html>
PHP code is as follows
<?php
header("Content-type:text/json;charset=utf-8");
$num = filter_input(INPUT_GET, 'num', FILTER_SANITIZE_STRING);
//$num=$_GET["num"];
class p_mysqli extends mysqli {
public function __construct($host, $user, $pass, $db) {
parent::__construct($host, $user, $pass, $db);
if (mysqli_connect_error()) {
die('连接数据库发生错误咯: (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
}
}
$mysqli=new mysqli('127.0.0.1', 'root', 'wl123456','proxys');
if ($mysqli->connect_errno) {
die('Connect Error:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$sql = "SELECT id,mac,name,password FROM s_user WHERE id = ?";
//$sql = "SELECT mac,name,password FROM s_user WHERE id = '207545'";
// 获取预处理
$stmt = $mysqli->prepare($sql);
// 绑定参数
$stmt->bind_param("s", $num);
// 执行
$stmt->execute();
// 获取结果集
$result = $stmt->get_result();
// 获取数据
$data = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);
?>
console.log(data) found that the array is empty
The HTML page displays the result like this: mac:undefined
It feels like the request did not return data. Use browser debugging tools to see the return value of the request
You have stated all the problems. The data returned by the background is empty. Judge the data. If it is empty, do not follow the following logic! You used the non-existent key value mac in js, it must be undefined
Print the variable data before the last echo on the PHP page to see if there is a value. If there is no value, print the value of result on it to locate the problem step by step