2

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?

5
  • This error due to incorrect password or some like that Commented Jul 24, 2015 at 9:37
  • 1
    You mix mysql_* and mysqli_* Commented Jul 24, 2015 at 9:38
  • first try to login phpmyadmin it's work fine. if not then let me know I give you solution Commented Jul 24, 2015 at 9:39
  • possible duplicate of Fix Access denied for user 'root'@'localhost' for phpMyAdmin Commented Jul 24, 2015 at 9:43
  • @AdamJ please also vote up my answer. If you found it useful. Commented Jul 24, 2015 at 10:03

1 Answer 1

1

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!!!!!!!!";
}
Sign up to request clarification or add additional context in comments.

3 Comments

This will still result in NO as mysql_num_rows($query) is simply wrong
@GerdK I already have changed to if(mysqli_num_rows($query) but now also mentioned in my answer.
@GerdK opps sorry got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.