簡體   English   中英

使用PHP和MySQL的“文本”列中帶有特殊字符的數據進行獲取

[英]Fetch data with PHP and MySQL of a “text” column with special characters on it

我有一個MySQL數據庫,該數據庫的其中一個表(theTable)上具有此定義

CREATE  TABLE `theDatabase`.`theTable` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `thecolumn` TEXT NOT NULL ,
  PRIMARY KEY (`id`) );

我正在嘗試使用PHP:

/*File executeSelect.php*/
mysql_connect("theServer","theUser","thePassword") or die(mysql_error());
mysql_select_db("theDatabase") or die (mysql_error());
$q = mysql_query($_REQUEST['query']);
while($e = mysql_fetch_assoc($q))
    $output[] = $e;
print(json_encode($output));
mysql_close();

如果我做了executeSelect.php?query=select * from theTable ,該列中顯示的信息thecolumnNULL如果thecolumn有特殊字符。 我將thecolumn定義為text因為我需要在其上存儲大文本( thecolumn個字母)。

例如, executeSelect.php?query=select * from theTableid=1;thecolumn=asdftheTable工程確定,但具有相同的ExecuteSelect id=1;thecolumn=ásdftheTableNULLthecolumn

我試圖使用MySQL控制台執行相同的查詢,並且工作正常。 我正在使用Apache 2.4和PHP 5.3。

我在這里誤解是我的php代碼無法以正確的方式從MySQL的text列中檢索數據嗎?

我強烈建議不要這樣做,因為這種方法很容易遭受SQL注入攻擊

代碼不應該是:

while($e = mysql_fetch_assoc($q))
    $output[] = $e['thecolumn'];
print(json_encode($output));
mysql_close();

我不推薦這種方法,建議也使用PDO

兩年后,困擾我的是這個問題沒有答案可以解決我的問題。

正如Mike W在問題注釋中指出的那樣,問題出在sql客戶端中的編碼。

如果使用MySQLi,則可以使用PHP中描述的內容:mysqli :: set_charset-Manual

在我的場景中,只需要在打開連接后$mysqli->set_charset("utf8")執行一個簡單的$mysqli->set_charset("utf8")

暫無
暫無

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

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