PHP 将 implode() 与 mysql_real_escape_string() 结合使用时出现问题
如何正确编写同时内爆和 mysql_real_escape_string 的 MySQL 查询代码?
这是我的代码,但失败了:
$sql = "INSERT INTO BEER_LOCATIONS
(LOCATION_NAME, LOCATION_STREET, LOCATION_CITY, LOCATION_STATE,
LOCATION_ZIPCODE, LOCATION_PHONE, BEER_STYLE )
VALUES
('" . mysql_real_escape_string(implode("', '", $row)) . "')";
谢谢
How do I correctly write the code for a MySQL query that both implodes and mysql_real_escape_string?
This is the code that I have, but it's failing:
$sql = "INSERT INTO BEER_LOCATIONS
(LOCATION_NAME, LOCATION_STREET, LOCATION_CITY, LOCATION_STATE,
LOCATION_ZIPCODE, LOCATION_PHONE, BEER_STYLE )
VALUES
('" . mysql_real_escape_string(implode("', '", $row)) . "')";
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会将 mysql_real_escape_string 应用于 $row 中的每个元素,并将包含转义值的数组返回到 implode。
This applies
mysql_real_escape_string
to every element in$row
and returns an array with the escaped values toimplode
.尽管这是一个非常老的问题,Google 仍然把我带到这里,所以也许我的解决方案对其他人有帮助:
当尝试使用 MySQLi 版本的 mysql_real_escape_string 时,你会意识到 array_map 无法处理所需的两个参数对于 mysqli_real_escape_string。因此,您必须将其包装在一个仅接受一个参数的函数中:
然后您可以在 array_map 中使用该函数,如下所示:
希望这会有所帮助。
Even though this is a very old question, Google still brought me here, so maybe my solution will be helpful to someone else:
When trying to use the MySQLi version of mysql_real_escape_string, you'll realise that array_map can't handle the two parameters required for mysqli_real_escape_string. Hence you have to wrap it in a function that only takes one parameter:
Then you can use that function in array_map like so:
Hope this helps.