0

Hi,
I've been searching for a solution for this problem, but couldn't find anything that would help me. I get this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\lab2.andornagy.info\lib\core\login.php on line 15

My codes:

Login.php:

<?php 

include_once("../autoload.php");

$login = new Login();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="login.php" method="get" target="_self">

<input name="login" value="true" type="hidden" />

<input name="user" type="text" />

<input name="pass" type="password" />

<input name="" type="submit" value="Login" />

</form>

<?php

if ( isset($_GET['login']) ) {

    $login->DoLogin();

        }

?>


</body>
</html>

The autoload works just fine.

The login.php for Login Class:

<?php

class Login extends Admin {

    public function DoLogin($user = null, $pass = null) {

        $user = $_GET['user'];
        $pass = $_GET['pass'];

        $user = mysql_real_escape_string($user);
        $pass = mysql_real_escape_string($pass);

        $query = "SELECT * FROM account WHERE username='$user' and password='$pass'";
        $result = mysql_query($query);
        $num_rows = mysql_num_rows($result);

        if ( $num_rows==1 ) {

            echo 'Login Successfull.'; 
            session_start();
            $_SESSION['user'] = $user;
            header("refresh:0;url=index.php");

        } else {

            die('Wrong Password or Username');

            }

        }

    public function LoginCheck() {

        session_start();
        if ( isset($_SESSION['username']) ){  
        } else {
        die(header('location:login.php'));
        }
    }

    }
2
Contributors
4
Replies
10
Views
4 Years
Discussion Span
Last Post by NagyAndor1995
0

Your query failed for some reason. I notice you didn't provide a connection as the second argument to mysql_query(), have you opened a connection to the database already?

0

Yes, I opened on in an other file, and an otehr class. Do I need to open one in this class aswell?

0

Yes, I opened on in an other file, and an otehr class. Do I need to open one in this class aswell?

I'd keep connections local to the class that uses them, but that's because I'm stupid and need the extra help. ;) Provided you open the connection before making calls to mysql_query(), it should be okay, but you do need to consider order of operations since it's not super explicit.

Your query is simple, but it still couldn't hurt to echo the final string that gets passed to mysql_query() and run it manually from MySQL just to be sure it works the way you think it works.

Also, include error checking with mysql_error() for more details about the problem:

$result = mysql_query($query);

if (!$result) {
    die('Error: ' . mysql_error());
}

...

Edited by deceptikon

0

Yes the problem was the Mysql class wasn't called to run, so no connection was made. Anyways, How can I use some variables that are defined in an separte file, within a class?
for ex:

lib/config.php

<?php 
// -----------------------------------------------------------------------------------
// Defineing all the directories and veriables that are needed for our system to work.
// -----------------------------------------------------------------------------------

// MySQL Configurations 

    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db   = 'lab2';

// Protecting the Mysql Configurations

    $mysqlhost = mysql_real_escape_string($host);
    $mysqluser = mysql_real_escape_string($user);
    $mysqlpass = mysql_real_escape_string($pass);
    $mysqldb = mysql_real_escape_string($db);

and I'd like to use them in an other file which is in lib/core/admin.php

<?php

include_once('../lib/config.php');

class Admin {

    public static function mysqlCon($mysqlhost = null, $mysqluser = null, $mysqlpass = null, $mysqldb = null) {

        $con = mysql_connect($mysqlhost, $mysqluser, $mysqlpass);

        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($mysqldb, $con);

        print ('Done');

        }
    } // Admin Class    

.

Edited by NagyAndor1995

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.