MySQL将URL从PHP传递到数据库

时间:2019-02-22 01:04:51

标签: php mysql url escaping quotes

在我的php代码中,我有一个SQL字符串select * from tablename where url in (%s);

我正在从urls构建输入数组,它是URL字符串数组。 $urls = array("https://www.example.com", "https://www.example.com/try002");

我尝试了多种方法,但是没有运气。

方法1:

$inputParams = array(implode(", ", $urls)); 

这给了我You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '://www.example.com,https://www.example.com/photos/try002)'

的错误

方法2: 因此,我尝试同时添加双转义和单转义引号,因为在sql db中尝试使用select * from tablename where url in ("https://www.example.com", "https://www.example.com/blabla")时可以。

foreach($urls as &$url)
{
   $url = "'" . urlencode($url) . "', ";
}
$inputParams = array($urls);

双引号或单引号均无效。它产生对select * from tablename where url in ( \“ https://example.com \”,"www.example.com/blabla")

的查询

我应该如何制定我的输入网址,以便获得类似 select * from tablename where url in ("https://www.example.com", "https://www.example.com/blabla")

非常感谢。

0 个答案:

没有答案