我想运行一个MySQL查询wherey部分正在使用LIKE查找。使用数组创建一个动态MySQL查询 - PHP的
表结构:TableExample
id name piece
0 jon piece0
1 james piece3
2 sarah piece6
将剪掉我到目前为止有:
$pieces = "piece0 piece1"; //variable
$piecearrayexplode = explode(" ", $pieces);
$piece0 = $piecearrayexplode[0];
$piece1 = $piecearrayexplode[1];
$sql = "SELECT * FROM TableExample WHERE piece LIKE '%$piece0%' OR pieces LIKE '%$piece1%'";
我的问题是,$件是一个变量,我需要$ SQL是动态的,自动具有正确数量的LIKE语句。
E.g.如果$ pieces =“piece0 piece1 piece2”,我希望$ sql是:
$sql = "SELECT * FROM TableExample WHERE piece LIKE '%$piecearrayexplode[0]%' OR pieces LIKE '%$piecearrayexplode[1]%' OR pieces LIKE '%$piecearrayexplode[2]%'";
注意:$ pieces总是以空格分隔。
我可以做一个字数。
$count = str_word_count($pieces);
我不知道该从哪里去。
我没有看这个Create a dynamic mysql query using php variables
它似乎没有什么我要找的,因为喜欢是连续的,而不是像1 WHERE单个语句。我在这里错过了什么吗?