0

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

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

1 回答 1

1

爆炸词

$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 - 它已被弃用。

于 2011-11-14T14:26:19.830 回答