在数组 PHP 中显示数据库值 MYSQLi
Display database value in array PHP MYSQLi
我正在尝试显示我从 ajax 发送到 php 文件的一些数据,但是由于某种原因它没有在页面上显示它。它的工作方式我在输入字段中输入搜索词,然后 ajax 脚本 post 将值输入 php 脚本,return 数据库值请求返回。
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
//require '../db/connect.php';
$con = mysqli_connect("localhost","root","root","retail_management_db");
$name = mysqli_real_escape_string($con,trim($_POST['name']));
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = {$name}";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$loc = $row['location'];
echo $loc;
}//close While loop
} else {
echo $name . "Name not Found";
}
}
html 形式:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Retail Management Application</title>
</head>
<body>
Name: <input type="text" id="name">
<input type="submit" id="name-submit" value="Grab">
<div id="name-data"></div>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/global.js"></script>
</body>
</html>
您正在向查询附加 MySQL 错误结果,并且您正在尝试查询查询结果,请尝试以下操作:
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = '$name'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
编辑:
{$name}
这是一个字符串,应该用引号代替。
在 where
子句中将其更改为 '$name'
。
使用:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
将为您提供查询失败的原因。
我正在尝试显示我从 ajax 发送到 php 文件的一些数据,但是由于某种原因它没有在页面上显示它。它的工作方式我在输入字段中输入搜索词,然后 ajax 脚本 post 将值输入 php 脚本,return 数据库值请求返回。
error_reporting(E_ALL);
ini_set('display_errors', '1');
if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
//require '../db/connect.php';
$con = mysqli_connect("localhost","root","root","retail_management_db");
$name = mysqli_real_escape_string($con,trim($_POST['name']));
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = {$name}";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$loc = $row['location'];
echo $loc;
}//close While loop
} else {
echo $name . "Name not Found";
}
}
html 形式:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Retail Management Application</title>
</head>
<body>
Name: <input type="text" id="name">
<input type="submit" id="name-submit" value="Grab">
<div id="name-data"></div>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/global.js"></script>
</body>
</html>
您正在向查询附加 MySQL 错误结果,并且您正在尝试查询查询结果,请尝试以下操作:
$query = "SELECT `names`.`location` FROM `names` WHERE`names`.`name` = '$name'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
编辑:
{$name}
这是一个字符串,应该用引号代替。
在 where
子句中将其更改为 '$name'
。
使用:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
将为您提供查询失败的原因。