我的数据库查询有问题。当我尝试提取之前空格转换为 %20 的行时,查询返回 null。我测试了其他行都工作正常,为什么会出现这个问题? 我是否需要手动删除每行中的 %20 才能使其正常工作?
谢谢!
<?php
include_once 'includes/db_connect.php';
$query = "SELECT * FROM USERS WHERE LIKES= '$_GET[imgname]' ";
//Creating resonse json array, with another array inside
$jsonResponse = array( "info" =>array() );
if($result = mysqli_query($mysqli, $query)) {
while($row = mysqli_fetch_assoc($result)){
$jsonRow = array(
'names' => $row['USERNAME'],
'userIds' => $row['USERID']
);
//adding the $jsonRow array to the end of the "users" array as key/value
array_push($jsonResponse["info"], $jsonRow);
}
}
//encoding to json for the app
echo json_encode($jsonResponse);
?>
将 SQL 查询更改为:
$query = "SELECT * FROM USERS WHERE LIKES= '".rawurlencode($_GET[imgname])."' ";
LIKES
列具有编码的图像路径(空间转换为%20
)。因此,应对 $_GET[imgname]
进行编码以匹配数据库记录。