0

我有一个如下所示的数组:(1, 4, 2, 3)。我想选择包含列 tid 的所有线程,数组中的这些数字之一:

$get = $pdo->prepare("SELECT * FROM mybb_threads ORDER BY replies DESC LIMIT 10");
$get->execute();                    
$array = $get->fetchAll(PDO::FETCH_COLUMN, 0);

$new = $pdo->prepare("SELECT * FROM mybb_posts WHERE replyto = 0 AND tid IN (:ids) LIMIT 10");
$new->execute(array(":ids" => implode(",", $array)));

但是,它只从数据库中获取一项,而我有多个结果?

为什么这样做?

我认为它只获取第一个数组项,我不确定,但你发现有什么问题吗?

我想做的是:

我有标签。

选项卡按回复显示最受欢迎的主题。我想这样做,当您单击选项卡时,它将切换到正确的帖子。

<div class="container row">
    <h2>Top 10 Stories</h2>
    <div id="story-tab">
        <ul>
            <?php
            $test = $pdo->prepare("SELECT * FROM mybb_threads ORDER BY replies DESC LIMIT 10");
            $test->execute();

        //  while ($row = $test->fetch(PDO::FETCH_ASSOC))
            //{
                //echo'<li><a href="#st-'.$row['tid'].'-'.$row['fid'].'">'. $row['subject'].'</a></li>';
                echo'<li><a href="#st-1-1">1</a></li>';
                echo'<li><a href="#st-2-1">2</a></li>';
            //} 
            $get = $pdo->prepare("SELECT * FROM mybb_threads ORDER BY replies DESC LIMIT 10");
            $get->execute();                    
            $array = $get->fetchAll(PDO::FETCH_COLUMN, 0);

            ?>
        </ul>
        <?php
            $test = $pdo->prepare("SELECT * FROM mybb_threads ORDER BY replies DESC");
            $test->execute();

            $fetch = $test->fetch(PDO::FETCH_ASSOC);

            $new = $pdo->prepare("SELECT * FROM mybb_posts WHERE replyto = 0 AND tid IN (:ids) LIMIT 10");
            $new->execute(array(":ids" => implode(",", $array)));

            while ($row = $new->fetch(PDO::FETCH_ASSOC))
            {
                echo '<br /><br /><br /><br />'. $row['tid'].','.$row['fid'];
            }
        ?>
    </div>
    <br class="clear">
    <h2>Top 10 Popular Threads</h2>
    <div id="thread-tab">

    </div>
</div>

有任何想法吗?

4

2 回答 2

0

Instead of..

$new->execute(array(":ids" => implode(",", $array)));

could you try

$ins= implode(',', array_fill(0, count($array), '?'));

and then

$new->prepare("SELECT *  FROM mybb_posts WHERE replyto =0 AND tid IN ($ins) LIMIT 10");
$new->execute($ins);
于 2013-06-14T15:42:16.773 回答
0

就在这里http://php.net/manual/en/pdostatement.execute.php 示例 #5展示了如何使用整数数组准备带有 IN 子句的语句。对于您的情况,这将是...

$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));

$sth = $dbh->prepare("SELECT * FROM mybb_posts WHERE replyto = 0 AND tid IN ($place_holders) LIMIT 10"); 
$sth->execute($params); // fills the placeholder ?'s with the values of your array

希望这可以帮助。

于 2013-06-14T15:34:31.327 回答