这是我的 PHP 代码:
$json = file_get_contents('php://input');
$userInfo = json_decode($json);
$id = $userInfo->id_user;
include "DatabaseManager.php";
$db = new DatabaseManager();
$username = $db->getInfoUser($id);
和
function getInfoUser($id)
{
$connection = mysqli_connect("localhost", "root", "", DatabaseManager::DATABASE_NAME);
$sqlCommand = "SELECT username FROM users WHERE id = '$id'";
$result =$connection->query($sqlCommand);
if($result->num_rows >0){
while ($row = $result->fetch_assoc()){
echo json_encode(['username' => $row['username'],"url_profile_img"=> $row['img_profile']]);
}
}
}
此代码返回 $row['username']
的值没有问题,但是 $row['img_profile']
返回 null!
数据库中的 img_profile
存在但返回 null。
我该怎么办?
最佳答案
您没有在 SQL 中选择 img_profile...
SELECT username FROM users WHERE id = '$id'
也许
SELECT username, img_profile FROM users WHERE id = '$id'
同样值得研究如何使用准备好的语句来帮助保护您的代码。
关于php - 找不到行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52907547/