简体   繁体   English

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

[英]MySQL query returning blank rows, even though I don't have blank rows

I have a search query that returns the results from the table, however it also returns blank rows.我有一个搜索查询,它返回表中的结果,但它也返回空白行。 In my table, I don't have blank rows.在我的表中,我没有空白行。

I have tried to limit the results to show 1, but it doesn't seem to have any affect.我试图将结果限制为显示 1,但它似乎没有任何影响。 Would it be that I don't have a "if 0 results then...."?会不会是我没有“如果结果为 0 则......”?

php code: 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);
?>

The issue was the UTF8, i edited my code to the following:问题是 UTF8,我将代码编辑为以下内容:

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

To find this issue, I added the following to my script to show what the error was for the blank values:为了找到这个问题,我在脚本中添加了以下内容,以显示空白值的错误:

Just after the json_encode() function that you have:就在您拥有的 json_encode() function 之后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM