[英]Fetch data with PHP and MySQL of a “text” column with special characters on it
I've a MySQL database that has this definition on one of its tables (theTable) 我有一个MySQL数据库,该数据库的其中一个表(theTable)上具有此定义
CREATE TABLE `theDatabase`.`theTable` (
`id` INT NOT NULL AUTO_INCREMENT ,
`thecolumn` TEXT NOT NULL ,
PRIMARY KEY (`id`) );
I'm trying this with PHP: 我正在尝试使用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();
If I do a executeSelect.php?query=select * from theTable
, the information shown for the column thecolumn
is NULL
if thecolumn
has special characters. 如果我做了
executeSelect.php?query=select * from theTable
,该列中显示的信息thecolumn
是NULL
如果thecolumn
有特殊字符。 I've thecolumn
defined as text
because I need to store large texts on it (500~1000 letters). 我将
thecolumn
定义为text
因为我需要在其上存储大文本( thecolumn
个字母)。
For example, a executeSelect.php?query=select * from theTable
with id=1;thecolumn=asdf
in theTable
works OK, but the same executeSelect with id=1;thecolumn=ásdf
in theTable
gives NULL
for thecolumn
. 例如,
executeSelect.php?query=select * from theTable
与id=1;thecolumn=asdf
在theTable
工程确定,但具有相同的ExecuteSelect id=1;thecolumn=ásdf
在theTable
给NULL
的thecolumn
。
I've tried to execute the same query using the MySQL console and it works OK. 我试图使用MySQL控制台执行相同的查询,并且工作正常。 I'm using Apache 2.4 and PHP 5.3.
我正在使用Apache 2.4和PHP 5.3。
What I'm misshing here that my php code doesn't retrieve data from text
columns of MySQL in a right way? 我在这里误解是我的php代码无法以正确的方式从MySQL的
text
列中检索数据吗?
我强烈建议不要这样做,因为这种方法很容易遭受SQL注入攻击
Should the code not be: 代码不应该是:
while($e = mysql_fetch_assoc($q))
$output[] = $e['thecolumn'];
print(json_encode($output));
mysql_close();
I would not recommend this method, and would recommend using PDO also 我不推荐这种方法,建议也使用PDO
After 2 years, it bothers me that there is no answer in this question that solves my issue. 两年后,困扰我的是这个问题没有答案可以解决我的问题。
As pointed by Mike W in the comments of the question, the issue was with encoding in the sql client. 正如Mike W在问题注释中指出的那样,问题出在sql客户端中的编码。
If using MySQLi, something like described in PHP: mysqli::set_charset - Manual can be used. 如果使用MySQLi,则可以使用PHP中描述的内容:mysqli :: set_charset-Manual 。
In my scenario, it was just a matter of doing a simple $mysqli->set_charset("utf8")
right after opening the connection. 在我的场景中,只需要在打开连接后
$mysqli->set_charset("utf8")
执行一个简单的$mysqli->set_charset("utf8")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.