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

(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-已弃用。

关于php - 如何在新查询中使用爆炸数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8122944/

10-11 02:53