我正在尝试从MySQL数据库中的表用户获取数据。使用下面的代码我能够登录到数据库(不会返回错误),但是当我尝试从表中读取数据时它会显示警告(在这种情况下,我只是选择行来检查是否代码工作)。代码如下:
<?php
$host_name = "localhost";
$database = "aj_database";
$user_name = "aj_user";
$password = "password";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno())
{
echo "The connection failed: " . mysqli_connect_error();
}
$username = $_GET["username"];
$password = $_GET["password"];
$query = ("SELECT * FROM users WHERE username = 'admin'");
$insert = mysql_query ($query);
if(mysql_num_rows($query) == 0) {
echo "NO";
} else {
echo "YES!!!!!!!!";
}
?>
使用上面的代码,我得到以下结果:
警告:mysql_query()[function.mysql-query]:拒绝访问用户&root;&#39; @&#39; localhost&#39; (使用密码:否)在第18行的/home/adamjudk/public_html/airlinesimulator/login.php中
警告:mysql_query()[function.mysql-query]:无法在第18行的/home/adamjudk/public_html/airlinesimulator/login.php中建立指向服务器的链接
警告:mysql_num_rows()期望参数1为资源,字符串在第20行的/home/adamjudk/public_html/airlinesimulator/login.php中给出
并且返回的结果是在这些错误
之后NO
我做错了什么?
答案 0 :(得分:1)
您使用mysql_query()
代替mysqli_query()
并使用if(mysql_num_rows($query) == 0)
代替if(mysqli_num_rows($query) == 0)
。
尝试快速更新的代码。
$host_name = "localhost";
$database = "aj_database";
$user_name = "aj_user";
$password = "password";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno())
{
echo "The connection failed: " . mysqli_connect_error();
}
$username = $_GET["username"];
$password = $_GET["password"];
$query = "SELECT * FROM users WHERE username = 'admin'";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) == 0)
{
echo "NO";
}
else
{
echo "YES!!!!!!!!";
}