我有一个php脚本从一个表LABCPD中查询,其中有一个条目,但是POSTMAN中的GET调用返回状态OK,没有错误,但不返回任何数据,没有语法错误或权限/访问问题:
脚本:
<?php
/**
* CPD Manual porject.
* Files will be hosted on mooneycallans.com server
* SQL inj. safe
*/
require_once 'headers.php';
// Add debugging output to console in POSTMAN and error log
error_reporting(E_ALL);
ini_set('error_reporting', 'E_ALL');
// Make suer the database on server we are reading / writing to as in UTF-8standard
mysqli_set_charset('m558405_tests','utf8');
require_once 'headers.php';
// Create a connection to the databse 'm558405_tests DB
$conn = new mysqli(hostName,usernamePlaceHolder,passwordPlaceHolder,databaseName);
$table = "LABCPD";
// Check DB connection and return a JSON error if cannot connect
if ($conn->connect_error) {
$errorResponse = array(
'error' => 'Connection failed',
'message' => $conn->connect_error
);
header('Content-Type: application/json');
http_response_code(500); // Set the HTTP response code to indicate an internal server error
echo json_encode($errorResponse);
exit(); // Terminate the script
}
if ($_SERVER['REQUEST_METHOD'] === 'GET'){
if(isset($_GET['id'])) {
/// Sfaely escape the user input and use a prepared statement
$id = $conn->real_escape_string($_GET['id']);
// Prepare an SQL statement with a placeholder
$stmt = $conn->prepare("SELECT * FROM $table WHERE id = ?");
// Bind the escaped user input to the placeholder in the SQL statement
$stmt->bind_param("i",$id);
// Execute the prepared statement
$stmt->execute();
// Get the result of the query
$result = $stmt->get_result();
// Fetch the data as an associative array
$data = $result->fetch_assoc();
// Close the prepared statement
$stmt->close();
} else {
$data = array();
// Prepare an SQL statement to fetch all rows
$stmt = $conn->prepare("SELECT * FROM $table");
// Execute prepared statement
$stmt->execute();
// Get the result of the query
$result = $stmt->get_result();
while ($d = $result->fetch_assoc()){
$data[] = $d;
}
// Close the prepared statement
$stmt->close();
}
// Return the JSON data
exit(json_encode($data, JSON_THROW_ON_ERROR, 512));
}
字符串
数据库有一个条目:x1c 0d1x
我的问题是,它看起来像是PHP代码片段的问题,还是我错过了其他东西?
1条答案
按热度按时间w8biq8rn1#
感谢您的指针。问题出在下面的代码行。当我删除JSON_THROW_ON_ERROR时,GET请求会回复来自该查询的所有数据。远程Web服务器运行PHP 7.0,JSON_THROW_ON_ERROR是在PHP 7.3上添加的;
字符串