开发者

MySQLi Prepared Statement Query Issue

开发者 https://www.devze.com 2022-12-30 14:33 出处:网络
I\'m relatively new to MySQLi prepared statements, and running into an erro开发者_StackOverflow中文版r. Take this code:

I'm relatively new to MySQLi prepared statements, and running into an erro开发者_StackOverflow中文版r. Take this code:

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->query("SELECT * FROM members WHERE username='$user' AND     password='$pass'"))
{
echo $stmt->num_rows;
}

This will display "1", as it should.

This next piece of code though, returns "0":

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?"))
{
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
echo $stmt->num_rows;
}

Any ideas why?


you need to call store_result() before you get the number of rows


$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?")) { $stmt->bind_param("ss", $user, $pass); $stmt->execute(); $stmt->store_result(); // add this line echo $stmt->num_rows; }

0

精彩评论

暂无评论...
验证码 换一张
取 消