我是PHP和MySQL的新手,试图从我的表中返回一行匹配来自HTML表单的输入。代码如下:无法从PHP请求中获取MySQL表中的数据
$connect = mysqli_connect('localhost', 'root', '', 'testdb')
OR die('Could not connect to the server' . mysqli_connect_error());
$query = ("SELECT * FROM users WHERE username = '$username'");
$result = mysqli_query($connect, $query);
$hits = mysqli_affected_rows($connect);
if ($hits < 1) {
echo "Incorrect password or username";
die("<br /><a href='index.php'>Try Again<a/>");
} else {
// other not relevant code here
}
的VAR $hits
总是回来为0,当有匹配的查询表中的数据。
var_dump($result)
回报:
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=>
int(4) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
缺少' '''在mysqli_connect(' 本地主机,'和'select'查询使用'mysqli_num_rows' – Saty
你检查数据库连接?此外,通过在打印检查$ username的值.. –
第一行有语法错误mysqli_connect('localhost',... –