简体   繁体   English

将PHP更新到5.4后在MySql查询中引用错误

[英]Quotes errors in MySql queries after update php to 5.4

I have an application made ​​in php and my database is mysql. 我有一个用php制作的应用程序,我的数据库是mysql。 For years the application worked correctly, but a few days ago I updated my version of XAMPP, and it was updated my version of PHP, now I have the 5.4. 多年来,该应用程序可以正常运行,但是几天前,我更新了XAMPP版本,并且更新了PHP版本,现在有了5.4。 The problem now is to query database. 现在的问题是查询数据库。 The application is for a hospital, and, for example, I have many patients with last names containing single quotes. 该应用程序是用于医院的,例如,我有很多患者的姓氏包含单引号。 For example: Claudio O'Connor. 例如:Claudio O'Connor。 When I perform an update on this table, with some patient containing double quotes in its name, obviously the application fails. 当我对此表执行更新时,某些患者的名字中包含双引号,显然应用程序失败。 For example: 例如:

UPDATE patients SET lastname = 'O'Connor' WHERE idPatient = 92565

I think this problem has to do with the deprecation of the magic quotes. 我认为这个问题与魔术引号的弃用有关。 The problem is that the application is immense, and I can not fix this problem by looking at all the queries one by one. 问题在于该应用程序非常庞大,我无法一一查看所有查询来解决此问题。 Is there any way to fix this problem in general? 通常有什么办法可以解决此问题? Thank you very much. 非常感谢你。

In PHP 5.4, the automatic magic quotes feature was removed from PHP. 在PHP 5.4中,自动魔术引号功能已从PHP中删除。 If you're running a version older than 5.4, you can set magic_quotes_gpc = On in the php.ini (or by using the following): 如果您运行的版本低于5.4,则可以在php.ini中将magic_quotes_gpc = On设置为启用状态(或使用以下命令):

ini_set('magic_quotes_gpc', '1');

If you're using 5.4+, you'll need to manually apply mysql_real_escape_string to all data. 如果您使用的是5.4+版本,则需要手动将mysql_real_escape_string应用于所有数据。 Note that for SQL injection prevention, you should not rely on magic quotes and instead use manual code to escape the string. 请注意,为防止SQL注入,您不应依赖魔术引号,而应使用手动代码对字符串进行转义。

Activating manually the magic_quotes_gpc or or sanitizing the inputs with mysql_real_string_escape are quick fixes that will work fine but you should really consider to use PDO prepared statements that don't have any SQL Injection issues. 手动激活magic_quotes_gpc或使用mysql_real_string_escape输入是可以很好用的快速修复,但是您应该真正考虑使用没有任何SQL注入问题的PDO准备的语句。

Here is an example of what it looks like 这是一个看起来像的例子

<?php
$insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)");
$insert->execute(array('apple', 'green'));
$insert->execute(array('pear', 'yellow'));

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Group values by the first column */
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
?>

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

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