我最近从正常陈述中更新了
$result = mysql_query("SELECT user_id from users WHERE user_id = '$user_id'");
准备好的陈述(安全)
prepare("SELECT user_id FROM users WHERE user_id = ?");
我遵循了一些教程,但这仍然无效:
public function isUserRegistered($user_id) {
print $user_id;
$stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
$stmt->bind_param("s", $user_id);
if ($stmt->execute()) {
$stmt->bind_result($user_id);
$stmt->fetch();
$no_of_rows = $stmt->num_rows;
print $no_of_rows;
$stmt -> close();
if ($no_of_rows > 0) {
print "Finally";
// user already registered
return true;
} else {
print "Stupid";
// user is not registered
return false;
}
}
}
提供的ID存在,因为我可以在打印的控制台中看到它。 if ($stmt->execute())
正在执行,但由于某种原因没有任何回复。
我该如何解决这个问题?如何打印结果?
我也试过了:
while ($stmt->fetch()) {
printf ("%s (%s)\n", $user_id);
}
答案 0 :(得分:2)
问题是$stmt->num_rows
无效,除非您首先存储结果,如下所示:mysqli_stmt::$num_rows - Return the number of rows in statements result set
fetch
正常工作并按预期返回数据行。唉,mun_rows
属性为零,因此isUserRegistered
方法失败。即使它返回了正确的数据。
注意:已更改为实际检查fetch
的返回值。
结论:
使用$stmt->store_result()
确保$stmt->num_rows
有用。
使用实际返回的数据确保它返回了您的预期?
示例代码:http://pastebin.com/wDvAru39
我使用的最终isUserRegistered
代码:
代码:
class UserRegister {
protected $conn = null;
public function __construct($dbConnection)
{
$this->conn = $dbConnection;
}
public function isUserRegistered($user_id) {
print $user_id;
$result_user_id = null;
$stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
$stmt->bind_param("s", $user_id);
if ($stmt->execute()) {
$stmt->store_result(); // need this to force the `num_rows` to be correct
$stmt->bind_result($result_user_id);
$stmt->fetch();
$no_of_rows = $stmt->num_rows;
var_dump(__METHOD__,
'input user id: '. $user_id,
'found user id: '. $result_user_id,
'reported number of rows: '. ($no_of_rows), __FILE__.__LINE__);
$stmt->close();
if (!empty($result_user_id)) { // check the returned data not the indicator
print "Finally";
// user already registered
return true;
} else {
print "Stupid";
// user is not registered
return false;
}
}
}
}
var_dump语句的输出:
注意:store_result
声明。
12321
string 'UserRegister::isUserRegistered' (length=30)
string 'input user id: 12321' (length=20)
string 'found user id: 12321' (length=20)
string 'reported number of rows: 1' (length=26)
string 'K:\developer\testmysql\index4.php78' (length=35)
Finally
string 'isUserRegistered :
评论' store_result'声明给出:
12321
string 'UserRegister::isUserRegistered' (length=30)
string 'input user id: 12321' (length=20)
string 'found user id: 12321' (length=20)
string 'reported number of rows: 0' (length=26)
string 'K:\developer\testmysql\index4.php79' (length=35)
Finally
string 'isUserRegistered : true
注意:报告的行数为零。
答案 1 :(得分:0)
大多数教程都是由知识比你的知识差的人编写的。在代码中使用num_rows()是这种教程的明确标志。
事实上,在PHP中,你不需要这样的功能。
以您的代码为例。 已经有一个变量可以判断用户是否已注册 - 选定的用户ID。您可以从该功能返回。或者你可以像这样使用一个常数值
public function isUserRegistered($user_id)
{
$sql = "SELECT 1 FROM users WHERE user_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("s", $user_id);
$stmt->execute();
$stmt->bind_result($found);
$stmt->fetch();
return $found;
}