2016-08-12 131 views
-3

我有这个疑问:PHP - 麻烦与SQL查询

SELECT * FROM items WHERE itemcategory= 123 AND itemname LIKE '%abc%'; 

我想传递参数给itemcategoryitemname;我试过这样的:

SELECT * FROM items WHERE itemcategory=".'$categoryid'." AND itemname LIKE" ."'%$itemname%'"." AND shopid=5003; 

它没有工作。谁能帮忙?

+3

这不是正则表达式。 –

+0

它以什么方式不起作用?你有没有得到一个特定的错误信息? –

+0

语法错误我相信。 –

回答

1

你在做什么几乎是正确的,但你可以去字符串连接复杂的是,如果你还记得$var在双引号字符串自动扩展

因此,这是更容易阅读和通知间距的问题,这是我认为这是不对您的发言

$q = "SELECT * 
     FROM items 
     WHERE itemcategory = '$categoryid' 
      AND itemname LIKE '%$itemname%' 
      AND shopid=5003"; 

假设你有这些变量的有效数据这应该工作

The only danger with doing this rather than using prepared parameterised queries is that you risk SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared statement and parameterized statements