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'));
        }
    }

    }

Recommended Answers

All 4 Replies

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?

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

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());
}

...

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    

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.