PHP-MySQLI准备语句

时间:2014-12-31 00:43:33

标签: php mysqli

我对编码非常陌生,我想为这段代码使用MySQLi准备语句,但是我花了好几个小时尝试,而我得到的只是一个白屏。

$queryLogin = "SELECT count(*) as count FROM tblUser WHERE username = '".$username."' and password = '".$password."' ";
$resultLogin = $mysqli->query($queryLogin);
$rowLogin = $resultLogin->fetch_assoc();

这是我到目前为止所尝试的

$query = "SELECT count(*) as count FROM `tblUser` WHERE `username`=? and `password`=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('ss',$username,$password);
$stmt->execute();
$result = $stmt->get_result();
$result->num_rows;
$rowLogin = $result->fetch_assoc();

2 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。

	$stmt = $mysqli->prepare("SELECT COUNT(*) FROM tblUser WHERE username  = ?
																	 AND password  = ?");
				$stmt->bind_param('ss', $username, $password);
				$stmt->execute(); 
				$stmt->bind_result($rowLogin);
				$stmt->fetch();
				$stmt->close();

答案 1 :(得分:0)

$stmt->execute();

添加

$stmt->store_result();

然后你应该找对了