Php Mysqli查询仅返回一行(最后一行)

Php Mysqli查询仅返回一行(最后一行),php,mysqli,Php,Mysqli,我正在尝试从我的数据库表中获取所有注释投票,该表属于$comments变量中的注释,该变量在comment\u投票表中称为item\u id,并且大于0。但是,当运行下面的脚本时,我只得到一个投票行的最后一个。根据下面的数据库表值,我认为应该有4个。您是只返回一行还是我做了其他错事 $comments = "1,3,4,5,6,7,11,12,13"; $db_conn = new Database; $stmt = $db_conn->connect->prepare("SELE

我正在尝试从我的数据库表中获取所有注释投票,该表属于$comments变量中的注释,该变量在comment\u投票表中称为item\u id,并且大于0。但是,当运行下面的脚本时,我只得到一个投票行的最后一个。根据下面的数据库表值,我认为应该有4个。您是只返回一行还是我做了其他错事

$comments = "1,3,4,5,6,7,11,12,13";

$db_conn = new Database;
$stmt = $db_conn->connect->prepare("SELECT ID FROM `COMMENT_VOTES` WHERE VOTE > 0 AND `ITEM_ID` IN (?)");
$stmt->bind_param("s", $comments);
$stmt->execute();
$result = $stmt->get_result();

$votes = [];

while ($row = $result->fetch_assoc()) { 

    array_push($votes, $row['ID']);

}

$votes = implode(",",$votes);
echo $votes;

+---------------------+
|    COMMENT_VOTES    |
+----+---------+------+
| ID | ITEM_ID | VOTE |
+----+---------+------+
| 1  | 12      | 1    |
| 2  | 8       | 0    |
| 3  | 3       | 1    |
| 4  | 22      | 1    |
| 5  | 5       | 0    |
| 6  | 5       | 1    |
| 7  | 5       | 1    |
| 8  | 5       | 0    |
+----+---------+------+

由于某种原因,我不能专心工作。但是,我使用$stmt=$db\u conn->connect->prepareselectiontem\u IDFROMCOMMENT\u votes,其中VOTE>0和tem\u IDIN$comment\u id,使它在没有绑定的情况下工作

我最初处理这个问题的方法与你的方法非常相似,就像你的方法一样,它失败了。一些研究表明,这种方法不起作用,因为我们都发现$comments字符串中的每一项都需要它自己的占位符和关联的类型字符串,这使我得出以下结论:

/* output */
$votes=array();

/* original string of IDS */
$comments='1,3,4,5,6,7,11,12,13';

/* create an array from IDS */
$array=explode(',',$comments);

/* create placeholders for each ID */
$placeholders=implode( ',', array_fill( 0, count( $array ), '?' ) );

/* create a type string for each - all going to be `i` */
$types=implode( '', array_fill( 0, count( $array ), 'i' ) );

/* create the sql statement */
$sql=sprintf( 'select `id` from `comment_votes` where `vote` > 0 and `item_id` in ( %s );', $placeholders );

/* create the prepared statement */
$stmt = $db->prepare( $sql );

/* Add the type strings to the beginning of the array */
array_unshift( $array, $types );

if( $stmt ){

    /* bind types and placeholders - 2nd arg passed by reference */
    call_user_func_array( array( $stmt, 'bind_param'), &$array );

    /* execute the query */
    $result=$stmt->execute();

    /* success */
    if( $result ){

        $stmt->store_result();
        $stmt->bind_result( $id );
        $rows=$stmt->num_rows;

        printf( 'rows found: %d<br />',$rows );

        /* add found IDs to output */
        while( $stmt->fetch() ) {
            $votes[]=$id;
        }
        /* tidy up */
        $stmt->free_result();
        $stmt->close();


        /* do something with output */
        printf( '<pre>%s</pre>', print_r( $votes, true ) );

    } else{
        exit('Error: No results');
    }

} else exit('Error: Prepared statement failed');

我希望有3个唯一的ID 3,5,12和4条记录,那么什么是get_result,它与记录集有什么关系呢?get_result;给我字段count 1和num_rows 1I我通常希望在执行准备好的语句并在执行完后绑定_result$id并尝试获取行计数之前看到$stmt->store_result如果我在1,3,4,5,6,7,11,12,13中这样做,它就可以正常工作。所以$comments运行为1,3,4,5,6,7,11,12,13或类似的值是一个问题。