【问题标题】:Mysqli query is only returning one row (last one)Mysqli 查询只返回一行(最后一行)
【发布时间】:2018-08-11 02:07:48
【问题描述】:

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

$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    |
+----+---------+------+

【问题讨论】:

  • 我希望 3 个唯一 ID 3,5,12 有 4 条记录,那么 get_result() 是什么,它对记录集有什么作用?
  • get_result();给我字段计数 1 和 num_rows 1
  • 我通常希望看到 $stmt->store_result() 在执行准备好的语句后尝试获取行数之前看到 bind_result( $id ) 之后
  • 如果我做 IN(1,3,4,5,6,7,11,12,13)​​ 它工作得很好。因此,$cmets 以“1,3,4,5,6,7,11,12,13”或其他方式运行是某种问题。

标签: php mysqli


【解决方案1】:

我最初的处理方法与您的方法非常相似,就像您失败了一样。一项小研究表明这种方法不起作用,因为我们都发现$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');

【讨论】:

    【解决方案2】:

    由于某种原因,我无法绑定工作。但是,我通过使用$stmt = $db_conn-&gt;connect-&gt;prepare("SELECTITEM_IDFROMCOMMENT_VOTESWHERE VOTE &gt; 0 ANDITEM_IDIN ($comment_ids)");

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 2013-11-06
      • 2013-09-12
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多