我刚接触PHP,我想制作一个简单的数据查找脚本。这是我现在在index.php文件中使用的代码。
<?php
$mysqlHost = 'localhost';
$mysqlUser = 'root';
$mysqlPass = 'password';
$mysqlDatabase = 'core';
$conn = new mysqli($mysqlHost, $mysqlUser, $mysqlPass, $mysqlDatabase);
if ($conn -> connect_error)
die('Connection Failed: ' . $conn -> connect_error);
$sql = 'SELECT * FROM users WHERE name = SimonM34';
$result = $conn -> query($sql);
if ($result -> num_rows > 0) {
$row = $result -> fetch_assoc();
$rawJson -> name = $row['name'];
$rawJson -> rank = $row['rank'];
$rawJson -> subrank = $row['subrank'];
$rawJson -> balance = $row['balance'];
$json = json_encode($rawJson);
echo $json;
} else {
echo 'No users found!';
}
$conn -> close();
?>
这总是显示No users found
,但是如果我将语句更改为SELECT * FROM users
,它将返回{"name":"SimonM34","rank":"Guest","subrank":"None","balance":"500"}
,这很奇怪,我无法弄清楚为什么会发生这种情况。
谢谢
答案 0 :(得分:0)
问题出在您的查询中:
$sql = 'SELECT * FROM users WHERE name = SimonM34';
您正在比较不带引号的字符串值。用以下内容替换您的查询:
$sql = 'SELECT * FROM users WHERE name = "SimonM34"';
那是我唯一的问题。