首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Mysqli查询仅返回一行(最后一行)

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

Stack Overflow用户
提问于 2018-03-02 19:42:21
回答 2查看 168关注 0票数 0

我试图从我的数据库表中获取所有评论投票,该表属于$comments变量(在comment_votes表中称为item_id )中的评论,并且大于0。但是,当运行下面的脚本时,我只得到一个投票行(最后一个)。根据下面的数据库表值,我认为应该是4。IN()只返回一行还是我做错了什么?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$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    |
+----+---------+------+
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-03 07:52:27

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/* 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');
票数 1
EN

Stack Overflow用户

发布于 2018-03-02 20:29:33

由于某种原因,我不能被绑定到工作中。但是,我通过使用$stmt = $db_conn->connect->prepare("SELECTITEM_IDFROMCOMMENT_VOTESWHERE VOTE > 0 ANDITEM_IDIN ($comment_ids)");在没有绑定的情况下让它工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49076247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文