Mysql真正转义字符串循环多个变量
假设我想插入名称、地址、城市、州、邮政编码值 $name、$address 等......
在插入之前如何在每个变量上运行 mysql_real_escape_string
。 必须有一个 foreach 或循环或 while 方法,而不是写出每个变量,对吧?
谢谢您的帮助。
汤姆
,如果我有的话
$data = array($address, $city, $name);
array_map('mysql_real_escape_string', $data);
,
$columns = "name, address, city, state, zip";
$count = $dbh->exec("INSERT INTO customer($columns) VALUES ($data)");
我会遇到很多错误。
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'@'localhost' (using password: NO) in /Users/tommyscott45/Sites/experiment/result.php on line 23
怎么办?
Say I want to insert into name, address, city, state, zip values $name, $address Etc.....
How can I run mysql_real_escape_string
on each of the variables before inserting. There has got to be a foreach or loop or while method instead of writing out each variable right?
Thanks for the help.
Tom
so if I have
$data = array($address, $city, $name);
array_map('mysql_real_escape_string', $data);
and
$columns = "name, address, city, state, zip";
$count = $dbh->exec("INSERT INTO customer($columns) VALUES ($data)");
I get a ton of errors.
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'root'@'localhost' (using password: NO) in /Users/tommyscott45/Sites/experiment/result.php on line 23
now what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试调用
mysql_real_escape_string
时发生数据库错误,并且我发现您正在使用$dbh->exec()
执行查询。 这表明您使用 PDO 连接到数据库,因此您应该使用PDO::quote
而不是mysql_real_escape_string
。此外,正如其他人已经提到的,解决问题的更好方法是使用准备好的语句和
PDO::prepare
。A database error has occurred when trying to invoke
mysql_real_escape_string
and I see that you're using$dbh->exec()
to execute the query. This suggests that you connect to the database with PDO, so you should rather usePDO::quote
instead ofmysql_real_escape_string
.Moreover, as others have already mentioned, a better way to solve your problem would be to use prepared statements and
PDO::prepare
.你用的是sprintf。
例如,
或者这不正是您正在寻找的; 避免一遍又一遍地输入“mysqli_escape_string”的方法。
You use sprintf.
For example
Or is that not exactly what you were looking for; a way to avoid typing "mysqli_escape_string" over and over again.
这应该有效。
但你真的不应该再使用 mysql 扩展了。 查看 PDO 或 mysqli 及其对“准备好的语句”的支持。
This should work.
But you really should not use the mysql extension anymore. Have a look at PDO or mysqli and their support for "prepared statements".
你有几个问题。
首先,您需要将 array_map() 的输出分配给变量,因为它不进行就地转换。 然后你需要将它内爆回字符串。
更大的问题是您手动组装 SQL,而不是使用数据访问层,数据访问层会获取您想要保存的信息,并使用有关您想要将其存储在何处的知识来组装正确的 SQL 语句,并使用正确的引用和所有。
顺便说一句,这也是建议使用准备好的语句背后的推动力,但仅仅使用准备好的语句只是解决方案的一半,因为您仍然需要组装 SQL 语句。
You have several problems.
First is that you need to assign the output of
array_map()
to a variable as it doesn't do in-place conversion. Then you need to implode it back to a string.The bigger problem is that you're hand assembling SQL instead of using a data access layer which would take the information you're wanting to save and use its knowledge about where you want to store it to assemble a correct SQL statement, with proper quoting and all.
This is also the impetus behind suggestions to use prepared statements, incidentally, but just using prepared statements is only half of the solution because you would still be assembling SQL statements.