MySQL 查询返回空行,即使我没有空行

问题描述

我有一个从表中返回结果的搜索查询,但它也返回空行。在我的表格中,我没有空白行。

我试图将结果限制为显示 1,但似乎没有任何影响。是不是我没有“如果 0 结果然后......”?

PHP 代码

<?PHP
 
// Create connection
$con=MysqLi_connect("server","user","password","database");
 
// Check connection
if (MysqLi_connect_errno())
{
    echo "Failed to connect to MysqL: " . MysqLi_connect_error();
}
 
// Select all of our stocks from table 'stock_tracker'
$sql = "
SELECT Task_title,task_description
  FROM TASKS
 ORDER 
    BY RAND () ASC 
 LIMIT 1
";
 
// Confirm there are results
if ($result = MysqLi_query($con,$sql))
{
    // We have results,create an array to hold the results
    // and an array to hold the data
    $resultArray = array();
    $tempArray = array();
 
    // Loop through each result
    while($row = $result->fetch_object())
    {
        // Add each result into the results array
        $tempArray = $row;
        array_push($resultArray,$tempArray);
    }
 
    // Encode the array to JSON and output the results
    echo json_encode($resultArray);
}
 
// Close connections
MysqLi_close($con);
?>

解决方法

问题是 UTF8,我将代码编辑为以下内容:

echo json_encode($resultArray,DEFINED('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0);

为了找到这个问题,我在脚本中添加了以下内容以显示空白值的错误:

就在您拥有的 json_encode() 函数之后:

echo json_last_error_msg(); // Print out the error if any
die(); // halt the script