gpt4 book ai didi

php - 无法使用 PHP 请求从 MySQL 表获取数据

转载 作者:行者123 更新时间:2023-11-29 21:39:13 26 4
gpt4 key购买 nike

我是 PHP 和 MySQL 的新手,并试图从我的表中返回与 HTML 表单的输入相匹配的行。代码如下:

$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) }

最佳答案

请发表用户名...并尝试此代码。

 $connect = mysqli_connect('localhost', 'root', '', 'testdb') 
OR die('Could not connect to the server' . mysqli_connect_error());

$username=$_POST['username'];

$query = ("SELECT * FROM users WHERE username = '$username'");

$result = mysqli_query($connect, $query);
$hits = mysqli_num_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
}

关于php - 无法使用 PHP 请求从 MySQL 表获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com