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

时间:2011-11-14 14:21:39

标签: php mysql exploded

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

(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>

1 个答案:

答案 0 :(得分:1)

爆炸词

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

转义并添加引号(php&gt; = 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 - 它已被弃用。

相关问题