PHP - strstr() 不能很好地与 MySql 配合使用
我的数据库中有这些表:
表 A:
id | haystack
-------------
1 | 682,401
2 | 351
表 B:
id | needle
-------------
1 | 352
2 | 682
3 | 978
我想做的就是检查表 A 中的大海捞针是否包含表 B 中的任何针。我用 PHP 编写了这段代码:
$res_A = mysql_query('SELECT * FROM table_A');
while($row_A = mysql_fetch_array($res_A)){
$res_B = mysql_query('SELECT * FROM table_B');
while($row_B = mysql_fetch_array($res_A)){
if(strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)){
echo 'I found this needle: '.$row_B['needle'].' in this haystack: '.$row_A['haystack'].'<br />';
}
}
}
但是,它不起作用。我一整天都想弄清楚,但没有机会。我需要提到的是,haystack 和 Needle 列都是 Varchar。
你能帮我解决这种情况吗? 提前致谢!
I have these tables in my DB:
TABLE A:
id | haystack
-------------
1 | 682,401
2 | 351
TABLE B:
id | needle
-------------
1 | 352
2 | 682
3 | 978
All I want to do is to check if a haystack from table A contain any needle from table B. I did this code in PHP:
$res_A = mysql_query('SELECT * FROM table_A');
while($row_A = mysql_fetch_array($res_A)){
$res_B = mysql_query('SELECT * FROM table_B');
while($row_B = mysql_fetch_array($res_A)){
if(strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)){
echo 'I found this needle: '.$row_B['needle'].' in this haystack: '.$row_A['haystack'].'<br />';
}
}
}
But, it doesn't work. I tried to figure it out all day, but no chance. I need to mention that the haystack and needle columns are Varchars.
Can you help me with this situation?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不把它们全部保存在 SQL 服务器上呢?更容易、更快。
从干草、针中选择 * WHERE hay.haystack LIKE CONCAT('%',needle.needle,'%');
Why don't you keep it all on the SQL server? Much easier and faster..
SELECT * from hay, needle WHERE hay.haystack LIKE CONCAT('%',needle.needle,'%');
注意
strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)
上的括号位置尝试:
if(strlen(strstr($row_A[ 'haystack'], $row_B['needle'])) > 0)
您正在包括
>; 0
在对strlen
的调用中Notice your Parenthesis placement on
strlen(strstr($row_A['haystack'], $row_B['needle']) > 0)
Try:
if(strlen(strstr($row_A['haystack'], $row_B['needle'])) > 0)
You are including the
> 0
in the call tostrlen
试试这个:
Try this: