如何在新查询中使用分解数据?

发布于 2024-12-15 04:10:45 字数 1290 浏览 0 评论 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>

I need to run a query on each part of the array coming out of explode.
Right now I just echo each word out from the explode, but I need to run a query like this for each word in the explode:

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

I am wondering how to deal with the separate queries that need to run. Can I combine then
in some way with the for loop?

I have check other posts, but nothing seems to address this directly.

Code Sample:I have 2 tables
phrases
words

<?

/*  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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-12-22 04:10:45

分解单词

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

Explode words

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

Escape and add quotes (php >= 5.3)

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

Run query with IN condition

mysql_query('SELECT * FROM words WHERE word IN ('. join(', ', $words) .')');

And you better use PDO or mysqli instead of mysql - it is deprecated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文