Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Mysql query is returning null
    text
    copied!<p>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. <br/><br/></p> <p>Here is my code.<br/></p> <pre><code>function login($user_name, $password) { $db1 = new DB_CONNECT(); $db1-&gt;connect(); $query = "SELECT user_name FROM users WHERE user_name = '$user_name'"; $result = $db1-&gt;makeAQuery($query); if (mysql_num_rows($result) == 1) { //check the password } else { //no user exist } } </code></pre> <p><br/> 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" <br/><br/></p> <p>Database class</p> <pre><code>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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload