我是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',... –