Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple PHP login function. Within this function, I am checking to see if the user exist by making a MySQL query that is selecting username from a table and counting the number of rows that is returned. I am having trouble with the query returning null when I know for a fact that the table contains the username.

Here is my code.

function login($user_name, $password) {
    $db1 = new DB_CONNECT();        
    $db1->connect();

    $query = "SELECT user_name FROM users WHERE user_name = '$user_name'";
    $result = $db1->makeAQuery($query);

    if (mysql_num_rows($result) == 1) {
        //check the password
    }
    else {
        //no user exist 
    }
}


I know that the username and password that are coming in are OK and I trim for any white spaces before I sent them to the function. I know that the database connection is working because it works on the rest of my PHP code. The only error that I am getting is one that mentions that the mysql_num_rows "expects parameter 1 to be resource, null given"

Database class

class DB_CONNECT {
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        mysql_select_db(DB_DATABASE, $con) or die(mysql_error());
    }
    function makeAQuery($query) {
        $result = mysql_query($query) or die(mysql_error());        
    } 
    function close() {
       // closing db connection
    mysql_close();
    }
}
share|improve this question
2  
what is the code for the DB_CONNECT class? – Kris Mar 14 at 13:39
2  
and p.s. xkcd.com/327 – Kris Mar 14 at 13:41
@Kris Nice one :) – Bigood Mar 14 at 13:42
1  
as above, but also STOP using mysql_* built in functions. php.net/manual/en/function.mysql-num-rows.php – djjjuk Mar 14 at 13:43
show 5 more comments

closed as not constructive by echo_me, NikiC, akond, Benjamin Gruenbaum, p.s.w.g Mar 14 at 18:54

As it currently stands, this question is not a good fit for our Q&A; format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

2 Answers

up vote 0 down vote accepted

This should work:

"SELECT user_name FROM users WHERE user_name = '".$user_name."'";

Also

change this:

function makeAQuery($query) {
    $result = mysql_query($query) or die(mysql_error());        
} 

to

function makeAQuery($query) {
    $result = mysql_query($query) or die(mysql_error());      
    return   $result;
} 
share|improve this answer
The Return statement did it. After all that, I forgot a simple statement. – Joshua Peters Mar 14 at 14:12
glad to help ;) – rinchik Mar 14 at 14:13

Maybe try removing the spaces between the = in the query.

"SELECT user_name FROM users WHERE user_name='$user_name'"

Your script may be returning null because the query could not run.

share|improve this answer
i can make it WHERE user_name = many_space_here '$user_name' and it will work – echo_me Mar 14 at 13:50
Ah sorry, I forgot MySQL does not care for whitespace. – George Mickleburgh Mar 14 at 13:54
With or without spaces, the samething – Joshua Peters Mar 14 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.