[英]What is the correct syntax for attaching a php handler to MySQL this?
Hey there I have this snippet and I am having trouble deducing the correct syntax for it. 嘿,我有这个代码段,并且在推断正确的语法时遇到了麻烦。 It is helping me search my DB and I cannot seem to get it.
它正在帮助我搜索数据库,但似乎无法获取它。 Any help is appreciated.
任何帮助表示赞赏。 Thanks!
谢谢!
Here is the snippet its a PHP portion searching my MySQL DB 这是其搜索我的MySQL DB的PHP部分的代码段
$query = mysql_query("SELECT e.event_name, v.venue_name FROM tie_in.events e LEFT JOIN tie_in.venues v ON v.venue_id = e.venue_id WHERE e.event_name = '%.$uniqueId.%' OR v.venue_name = '%.$uniqueId.%' ORDER BY e.event_time");
There area that I am having trouble with is the wrapping of the $uniqueId
handler. 我遇到的问题是
$uniqueId
处理程序的包装。 How would I wrap this properly with parentheses etc to have the proper syntax to complete the function? 我如何用括号等将其正确包装,以具有正确的语法来完成功能?
Thanks! 谢谢!
Pattern matches with %
wildcards need to use LIKE
, not =
. 具有
%
通配符的模式匹配需要使用LIKE
而不是=
。
You can interpolate PHP variables inside strings by enclosing them in curly braces. 您可以通过将PHP变量括在花括号中来在字符串内插值。
... WHERE e.event_name LIKE '%{$uniqueId}%' OR v.venue_name LIKE '%{$uniqueId}%' ...
Be careful that $uniqueId
is a safe variable to use in SQL. 请注意,
$uniqueId
是在SQL中使用的安全变量。 You don't want to introduce SQL injection vulnerabilities. 您不想引入SQL注入漏洞。
Also, for what it's worth, the mysql_* functions are deprecated , and will be removed from PHP in some future version. 另外,就其价值而言, 不建议使用mysql_ *函数 ,并将在以后的某些版本中将其从PHP中删除。 So I recommend you use PDO instead, unless you have to use mysql_* because you have a lot of legacy code.
因此,我建议您改用PDO,除非您必须使用mysql_ *,因为您有很多旧代码。
Either remove the %
since wildcards are not supported when you use =
请删除
%
因为使用=
时不支持通配符
$query = mysql_query("SELECT e.event_name, v.venue_name FROM tie_in.events e LEFT JOIN tie_in.venues v ON v.venue_id = e.venue_id WHERE e.event_name = '$uniqueId' OR v.venue_name = '$uniqueId' ORDER BY e.event_time");
Or use LIKE
instead of =
或使用
LIKE
代替=
$query = mysql_query("SELECT e.event_name, v.venue_name FROM tie_in.events e LEFT JOIN tie_in.venues v ON v.venue_id = e.venue_id WHERE e.event_name LIKE '%".$uniqueId."%' OR v.venue_name = '%".$uniqueId."%' ORDER BY e.event_time");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.