gpt4 book ai didi

php - mysqli 返回的行数不正确

转载 作者:搜寻专家 更新时间:2023-10-31 21:12:43 25 4
gpt4 key购买 nike

我正在为我的网站编写一个“赞”功能,但我终其一生都无法使用此功能!

假设这是我的数据库结构的简化版本:

id --- post_id --- user_id

1 --- 1 ----------- 1

2 --- 2 ----------- 1

这是我写的代码:

    public function userHasLikedPost($post_id,$user_id){
$this->post_id_clean = sanitize($post_id);
$this->user_id_clean = sanitize($user_id);

global $mysqli,$db_table_prefix;
$stmt = $mysqli->prepare("SELECT like_type FROM ".$db_table_prefix."post_likes WHERE user_id = ? AND post_id = ?");

$stmt->bind_param("ii", $this->post_id_clean, $this->user_id_clean);

$stmt->execute();
$stmt->store_result();

$stmt->bind_result($like_type);

$stmt->fetch();
$result;

if( $stmt->num_rows >0 ){
$result = array("liked" => 1, "like_type" => $like_type);
}else{
$result = array("liked" => 0);
}

return print_r($stmt);

$stmt->close();
}

当我调用 userHasLikedPost(1,1) 时,我的代码将返回正确行数

mysqli_stmt Object ( [affected_rows] => 1 [insert_id] => 0 [num_rows] => 1 [param_count] => 2 [field_count] => 1 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 5 ) 1

但是,如果我调用 userHasLikedPost(2,1),我将得到不正确的行数。

mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 2 [field_count] => 1 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 5 ) 1

为什么会这样?我是否正确使用 mysqli?数据存在于数据库中,如果我在 PHPMyAdmin 中手动运行查询,我将获得正确的输出。我花了大约 2 个小时试图让这个功能正常工作......在此先感谢您的任何回复:)

最佳答案

我已经解决了我的问题。我的绑定(bind)顺序不正确:/多么尴尬哈哈!

解决方法:

替换这行代码:

$stmt->bind_param("ii", $this->post_id_clean, $this->user_id_clean);

与:

$stmt->bind_param("ii", $this->user_id_clean, $this->post_id_clean);

关于php - mysqli 返回的行数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com