PHP 搜索查询结果不显示撇号

时间:2021-02-25 23:40:09

标签: php mysql json database

运行一个简单的搜索查询,它提供给一个 android studio textview。当我第一次设法通过 .php 站点显示结果时,所有特殊字符 ([{]}) 都显示出来,但我添加了这一行以删除:

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

然而,这现在意味着显示的结果也缺少特殊字符,主要是“'”。所以“你是”,实际上是在显示“你是”。

我尝试使用上面的 json 查询,但没有成功 - 知道我做错了什么吗?

为清晰起见,完整代码:

<?php

 // Create connection
$con=mysqli_connect("server","user","pass","table");
 
// 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, DEFINED('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0);
    
    echo json_last_error_msg(); // Print out the error if any
die(); // halt the script
}
 
// Close connections
mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:1)

我通过在搜索查询下方添加 $cValue=str_replace(array("'",",","."), array("&amp;#039;","&amp;#44;","&amp;#46;"), $_POST['contractValue']); 解决了这个问题。很有魅力!

相关问题