PHP异常不适用于num行或其他行

时间:2017-11-19 14:02:20

标签: php mysql sql prepared-statement

<?php 






global $connect;

# Get the username from the email session
# 

$email = $_SESSION['username'];

$get_username = $connect->prepare("SELECT username FROM users WHERE email = ? ");
$get_username->bind_param("s", $email);
$get_username->execute();
$get_username->bind_result($username);
$get_username->fetch();
$get_username->close();

# End username



# Get user referrals 
# Gets how many users have in table "referral" the username of the user requesting the information 


$get_referrals = $connect->prepare("SELECT username,balance FROM users WHERE referral = ? ");
$get_referrals->bind_param("s", $username);
$get_referrals->execute();
$amount = $get_referrals->num_rows;
$get_referrals->close();


# End user referrals




# How many referrals user have

echo $amount; 

# End how many

?>

这个查询不起作用,总是返回0并且应该返回5,有人可以向我解释这段代码发生了什么。 第一个查询$ username正确地返回我,只有第二个查询总是返回0并且在sql表中我在这个别名上有atlas 5引用

1 个答案:

答案 0 :(得分:0)

除非您在结束语句后立即致电$get_referrals->num_rows,否则

$get_referrals->store_result()将返回0。它返回零,因为结果仍未存储在内存中。这是store_result的作用,它将结果存储在内存中,以便num_rows可以返回返回的行数