Adam J
Adam J

Reputation: 45

MySQL: PHP: Cannot obtain data from table because of warnings

I am trying to obtain data from table users in MySQL database. Using the code below I am able to log in into the database (doesn't return an error) but it shows warnings when I am trying to read the data from the table (in this case I just select the row to check whether the code works). The code is as follows:

<?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!!!!!!!!";
}

?>

With the code above I get the following result:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'root'@'localhost' (using password: NO) in /home/adamjudk/public_html/airlinesimulator/login.php on line 18

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/adamjudk/public_html/airlinesimulator/login.php on line 18

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /home/adamjudk/public_html/airlinesimulator/login.php on line 20

and the result returned is after these errors is

NO

What am I doing wrong?

Upvotes: 2

Views: 130

Answers (1)

Hassaan
Hassaan

Reputation: 7682

You are using mysql_query() instead of mysqli_query()

and using if(mysql_num_rows($query) == 0) instead of if(mysqli_num_rows($query) == 0).

Try the quickly updated code.

$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!!!!!!!!";
}

Upvotes: 1

Related Questions