Php SQL get_result()返回空值?
我非常困惑为什么我的sql查询返回null,因为它实际上只是返回了正确的对象,我觉得我没有更改查询代码 我知道我的数据库正在运行b/c我网站的其他部分使用在Php SQL get_result()返回空值?,php,mysql,ajax,Php,Mysql,Ajax,我非常困惑为什么我的sql查询返回null,因为它实际上只是返回了正确的对象,我觉得我没有更改查询代码 我知道我的数据库正在运行b/c我网站的其他部分使用在configsql.php中创建的$mysqli,它们仍然在运行。我甚至在我的MAMP数据库上运行了id=2的相册中的SELECT*查询,它返回了正确的值!所以我不明白为什么我的代码中的sql查询返回null 如果有人能帮助我,我将不胜感激 $result = $stmt->get_result(); //returns null if
configsql.php
中创建的$mysqli
,它们仍然在运行。我甚至在我的MAMP数据库上运行了id=2的相册中的SELECT*查询,它返回了正确的值!所以我不明白为什么我的代码中的sql查询返回null
如果有人能帮助我,我将不胜感激
$result = $stmt->get_result(); //returns null
if($row = $result->fetch_assoc() && $result->num_rows === 1){ //which causes $row to be null
代码
基本上,我正在做的是,我有一个AJAX请求,它将信息发送到sqlinfo.php
,该请求在MAMP上调用sql数据库,并假设返回$info
数组上的数据库信息,然后将该信息打印在main.js
中的控制台上。由于某种原因,$info
数组总是有空元素:(
main.js
configsql.php
ysqli->prepare($queryFor[$requestType]);其中是$m@vSugumar抱歉,这是一个输入错误。我只是修改了它以反映我的代码中的内容
$(document).ready(function () {
$(".grid").on("click", ".edit", function (){
var albumId = $(this).siblings(".grid-info").attr("id");
var imageId = $(this).siblings("img").attr("id");
var request = (albumId != '') ? {requestType: 'Album', id: albumId} : {requestType: 'Image', id: imageId};
var getSQLInfo = $.ajax({
url: '../P3_M2/ajax/sqlinfo.php',
method: 'POST',
data: request,
dataType: 'json',
error: function(error) {
console.log(error);
}
});
getSQLInfo.success(function (sqlInfo){
console.log(sqlInfo); //every element in info array is nulll
});
});
});
<?php
define( 'DB_HOST', 'correct');
define('DB_NAME', 'correct');
define('DB_USER', 'correct');
define('DB_PASS','correct');
//Set up sqli
$mysqli = new mysqli( DB_HOST, DB_USER, DB_PASS, DB_NAME );
if ($mysqli->errno) { //Was there an error connecting to the database?
//The page isn't worth much without a db connection so display the error and quit
print($mysqli->error);
exit();
}
?>
<?php
require_once('../configsql.php');
$queryFor = array(
'Album' => 'SELECT * FROM Album WHERE id = ?',
'Image' => 'SELECT * FROM Image WHERE id = ?');
$requestType = filter_input(INPUT_POST, 'requestType', FILTER_SANITIZE_STRING);
if (empty($requestType)) {
echo 'Missing requestType.';
die();
}
$id = $mysqli->real_escape_string($_POST["id"]);
$info= array();
$stmt = $mysqli->prepare($queryFor[$requestType]);
$stmt->bind_param('i', $id);
$executed = $stmt->execute();
if(!$executed){
echo "SQL query $queryFor[$requestType] not executed";
exit();
}
$result = $stmt->get_result();
$stmt->close();
if($row = $result->fetch_assoc() && $result->num_rows === 1){
$info['id'] = $row['id'];
$info['title'] = $row['title'];
$info['date_created'] = $row['date_created'];
$info['creator'] = $row['creator'];
}
// Send back the array as json
echo json_encode($info);