如何在新查询中使用爆炸数据


How to use exploded data in new query?

我需要对数组的每个部分进行查询。
现在我只是从爆炸中回显每个单词,但是我需要对爆炸中的每个单词运行这样的查询:

(select * from words where word = $split_phrase[$i])

我想知道如何处理需要运行的单独查询。我可以把它们合并吗?以某种方式使用for循环?

我查看了其他帖子,但似乎没有直接解决这个问题。

代码示例:我有2个表短语单词

<?
/*  Table: phrases   */
$ID = 'ID'
$TARGET = 'TLP'
/*  Table: words   */
$ID_Split = 'ID';
$Word_Split = 'Word';
$WBW_Split = 'WBW';
?>

<table width="800">
<tr>
<th>Display Word</th>
</tr>
<?
/* I pass in a variable from another page which is inserted into the query */
    $phrase_query = mysql_query("SELECT * FROM phrases WHERE id=" . $_GET['id']);

/* Loop through the search results and explode the string on the column variable $Target */

    while($rows=mysql_fetch_assoc($phrase_query)) {
        $split_phrase = explode(" ", $rows[$TARGET]);

/* Loop through the string and list each word from the explode array / 
the html table in the echos is started outside the PHP tags */
    for($i = 0; $i < count($split_phrase); $i++)        {
        echo "<tr>";
        echo "<td>" .$split_phrase[$i] . "</td>";
        echo "</tr>";
                                                        }
?>
</table>

爆炸字

$words = explode(" ", $rows[$TARGET]);

转义并添加引号(php>= 5.3)

array_walk($words, function(&$word) {$word = '"' . mysql_real_escape_string($word) . '"';});

IN条件运行查询

mysql_query('SELECT * FROM words WHERE word IN ('. join(', ', $words) .')');
你最好使用PDO或mysqli而不是mysql -它已被弃用。