我想爲一個類進行登錄(所以我沒有考慮現在防止SQL注入),並且我在使用行計數來查看我是否可以登錄時遇到問題。PHP SQL row count
如何計算選擇的行數?我嘗試了一些不起作用的東西。
我得到的錯誤是在有評論的行中。
$host = "localhost";
$serverusername = "root";
$serverpassword = "";
$database = "usuarios";
$table = "user";
$username = $_POST['username'];
$password = $_POST['password'];
$mysqli = new mysqli($host, $serverusername, $serverpassword, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM ".$table." WHERE `username` = ".$username." AND `password` = ".$password;
$result = $mysqli->query($query);
$row = $result -> num_rows; //this line has an error. Trying to get property of non-object
if ($row != 0)
{
header('Location : index.html');
die();
}
else
{
echo "password incorrecta!";
}
$mysqli -> close();
您的代碼包含語法錯誤。 請查閱以下鏈接http://php.net/manual/en/mysqli.error.php和http://php.net/manual/en/function.error-reporting.php 並將其應用於您的代碼。 –
密碼可能是一個字符串。並看看準備好的語句 – Strawberry
爲了防止你的代碼從SQL注入,使用[PDO ::準備](http://php.net/manual/en/pdo.prepare.php) –