[英]My PHP code on the local server (Xampp) has the expected result, but on the online server it doesn't
Good afternoon people.大家下午好。
I created some functions in php / MySQL that returns a json object, As you can see in the code below.我在 php / MySQL 中创建了一些返回 json 对象的函数,如下面的代码所示。
File tweets.php (Class)文件 tweets.php(类)
<?php
class Tweets {
private $conn;
private $table_name = 'tweets';
public $id;
public $text;
public $created_at;
// Constructor
public function __construct($db) {
$this->conn = $db;
}
public function readAll() {
$query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at ASC";
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
public function create() {
$query = "INSERT INTO " . $this->table_name . " SET tweet_id = :tweet_id, text = :text, created_at = :created_at";
$stmt = $this->conn->prepare($query);
$this->tweet_id = htmlspecialchars(strip_tags($this->tweet_id));
$this->text = htmlspecialchars(strip_tags($this->text));
$this->created_at = htmlspecialchars(strip_tags($this->created_at));
$stmt->bindParam(':tweet_id', $this->tweet_id);
$stmt->bindParam(':text', $this->text);
$stmt->bindParam(':created_at', $this->created_at);
if($stmt->execute()) {
return $this->conn->lastInsertId();
}
return false;
}
public function delete() {
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $this->conn->prepare($query);
$this->id = htmlspecialchars(strip_tags($this->id));
$stmt->bindParam(1, $this->id);
if($stmt->execute()) {
return true;
}
return false;
}
public function deleteAll() {
$query = "DELETE FROM " . $this->table_name;
$stmt = $this->conn->prepare($query);
if($stmt->execute()) {
return true;
}
return false;
}
public function search($keywords, $start, $end){
$query = "SELECT * FROM " . $this->table_name . " WHERE text LIKE ? ORDER BY id ASC LIMIT ?, ?";
$stmt = $this->conn->prepare($query);
$keywords=htmlspecialchars(strip_tags($keywords));
$keywords = "%{$keywords}%";
$stmt->bindParam(1, $keywords);
$stmt->bindParam(2, $start, PDO::PARAM_INT);
$stmt->bindParam(3, $end, PDO::PARAM_INT);
$stmt->execute();
return $stmt;
}
public function count(){
$query = "SELECT COUNT(*) as total_rows FROM " . $this->table_name . "";
$stmt = $this->conn->prepare( $query );
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['total_rows'];
}
}
?>
File getTweets.php文件 getTweets.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
require_once '../config/config.php';
include_once '../config/database.php';
include_once '../objects/tweets.php';
$database = new Database();
$db = $database->getConnection();
$tweets = new Tweets($db);
$keywords = isset($_GET["s"]) ? $_GET["s"] : "";
$start = isset($_GET['start']) ? $_GET['start'] : '';
$end = isset($_GET['end']) ? $_GET['end'] : '';
$stmt = $tweets->search($keywords, $start, $end);
$num = $stmt->rowCount();
if($num > 0) {
$tweet_arr = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$tweet_item = array(
"text" => $text,
);
array_push($tweet_arr, $tweet_item);
}
http_response_code(200);
echo json_encode($tweet_arr);
} else {
http_response_code(404);
echo json_encode(
array("message" => "Nao foi achada nenhuma promoção.")
);
}
?>
The functions work perfectly on my local web server (Xampp), but when I upload to the final web server (Online), I have the response status 200 OK but without any json object.这些函数在我的本地 Web 服务器 (Xampp) 上完美运行,但是当我上传到最终的 Web 服务器(在线)时,我的响应状态为 200 OK 但没有任何 json 对象。
As you can see in the images below, the first is the response from the local server and the second from online.如下图所示,第一个是来自本地服务器的响应,第二个是来自在线的响应。 What could be happening?
会发生什么?
There could be errors that aren't being displayed, try adding the following code to the top of 'getTweets.php' and see if you get any PHP Warnings/Errors可能有未显示的错误,请尝试将以下代码添加到“getTweets.php”的顶部,看看您是否收到任何 PHP 警告/错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
据我所知,您没有将相同的参数传递给 Postman 中的在线测试,您缺少“开始”和“结束”,而且您似乎没有在 search() 方法中处理它们。
The chances are that you have a different default charset on the live sever vs local您可能在实时服务器与本地服务器上有不同的默认字符集
Check that you connection string contains the charset检查您的连接字符串是否包含字符集
mysql:host=HOSTNAME;dbname=DBNAME;charset=utf8;
After reading the PHP manual a lot I decided to create a function to handle the result.在阅读了很多 PHP 手册后,我决定创建一个函数来处理结果。 I did these two functions below, I hope it helps other people with the same problem that I had.
我在下面做了这两个功能,我希望它可以帮助其他有同样问题的人。
function safe_json_encode($value){
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
$encoded = json_encode($value, JSON_PRETTY_PRINT);
} else {
$encoded = json_encode($value);
}
switch (json_last_error()) {
case JSON_ERROR_NONE:
return $encoded;
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
case JSON_ERROR_UTF8:
$clean = utf8ize($value);
return safe_json_encode($clean);
default:
return 'Unknown error';
}
}
function utf8ize($mixed) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} else if (is_string ($mixed)) {
return utf8_encode($mixed);
}
return $mixed;
}
Call:称呼:
safe_json_encode($tweet_arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.