簡體   English   中英

PHP-獲取包含單引號的數據

[英]PHP - Fetching data that contains single quotes

在我的mysql數據庫中,我有一些包含單引號'文本,當我嘗試獲取該數據時,它顯示為null。 我該如何解決此問題。

謝謝

這是我在做什么

$products =   "SELECT * FROM products ";

$result = @mysql_query($products, $connection) or showSQLError();
    if($result) {
        $results_array = array();
        while($row = mysql_fetch_array($result)) {
            $results_array[] = array(
                'name' => $row['name'],
                'description' => mysql_real_escape_string(nl2br($row['description']))
            );
        }
    } 

而我的弦是

Puppy’s dental health. 

結果是

"description":null

這是一些固定的代碼:

  • 使用mysqli而不是mysql因此您使用的是更安全且得到更好支持的技術
  • 取消使用@禁止顯示錯誤消息; 抑制錯誤是PHP的愚蠢功能,這意味着使用該代碼的任何人都可能會收到PHP級別的警告,並且沒有意識到,從而可能導致混亂。
  • 刪除mysql_real_escape_string()可能會從輸出中刪除單引號

     $connection = mysqli_connect('host', 'user', 'password', 'database'); $result = mysqli_query($connection, $products) or die(mysqli_error($connection)); if ($result){ $results_array = []; while ($row = mysqli_fetch_array($result)){ $results_array[] = [ 'name' => $row['name'], 'description' => nl2br($row['description']), ]; } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM