1

I an trying to log into my website and I get the following error,

Warning: mysql_query() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\1\login.php on line 23

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\1\login.php on line 24

Warning: mysql_close() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\1\login.php on line 33

Here is the code for that page and the login session.

Login.php

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username = $_POST['username'];
$password = md5($_POST['password']);
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
// Selecting Database
$db = mysql_select_db("users");
// SQL qu00e020.ry to fetch information of registerd users and finds user match.
$query = mysql_query("select * from username where password='$password' AND username='$username'", $connection);
$rows = mysql_num_rows($query);
if(isset($_SESSION['login_user']))
session_destroy();
if ($rows == 1) {
$_SESSION['login_user']=$username; // Initializing Session
header("location: profile.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>

Session.php

<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "Oliver");
// Selecting Database
$db = mysql_select_db("users", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("select username from username where username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: home.php'); // Redirecting To Home Page
}
?>

Information The database is hosted locally with database name 'users' and table 'username'. When I login i get those errors and the page just reloads.

Update

changed to

$connection = mysql_connect("localhost", "root", "Oliver") or die (mysql_error);

No i get error

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\1\login.php on line 14

Notice: Use of undefined constant mysql_error - assumed 'mysql_error' in C:\xampp\htdocs\1\login.php on line 14
mysql_error
Oliver Ketley
  • 83
  • 1
  • 1
  • 13
  • Your connection is failing. Use `mysql_error` to see why. – andrewsi Jul 18 '15 at 16:11
  • If you are just starting out on a project please use `mysqli_` or `PDO` the `mysql_` extensions have been deprecated for years and as of PHP7 they have been removed – RiggsFolly Jul 18 '15 at 16:17
  • Thanks @Mureinik fixed with this post you attached, sorry for duplicate. – Oliver Ketley Jul 18 '15 at 16:18
  • Please [look at the manual](http://php.net/manual/en/function.mysql-connect.php) see how they check for errors after each call to any `mysql_` function call. If you do that you will be told what is wrong rather than having to guess – RiggsFolly Jul 18 '15 at 16:19
  • Been taught to use Mysql in college, which is stupid then ... Ill learn Mysql and POD thanks for the heads up, this project is an ongoing one, ill just update the code and make the necessary changes. Thanks – Oliver Ketley Jul 18 '15 at 16:19
  • use `mysqli_*` or `PDO` instead of `mysql_*`. – Alive to die - Anant Jul 18 '15 at 16:22

1 Answers1

3

This means that $connection is no valid connection, but a boolean. mysql_connect() returns false (a boolean) if the connection fails - otherwise it returns a ressource for further MySQL processing. You can see the error with the function mysql_error() and you can (because it is a boolean) do something like this:

$connection = mysql_connect("localhost", "root", "Oliver") or die(mysql_error());

Please note, that MySQL is deprecated in PHP 5, you should use MySQLi instead. It is not complicate but object-oriented (procedural style possible).

Richard
  • 2,840
  • 3
  • 25
  • 37
  • Thanks for the amazing explanation, I have so much code to rewrite the Mysqli will take forever :/ Ill mark your answer as correct in 4 minutes. Thanks. – Oliver Ketley Jul 18 '15 at 16:22
  • @OliverKetley: Yes, I know this problem ;-) But it is not that complicate. Just replace it ;-) `mysql_query` => `$db->query` / `mysql_fetch_object($sql)` => `$sql->fetch_object()` / ... I think there are even regular expressions to make this simple in the Internet ;-) – Richard Jul 18 '15 at 16:26
  • 1
    Thanks its all working now. thanks for the help. – Oliver Ketley Jul 18 '15 at 16:27