1

I keep on receiving an error message using mysql_num_rows(), can you help me figure out what went wrong on my code?

Here's my code:

<?php 
//check if the user press submit
if (isset($_POST['submit'] )) {

    $customer = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["username"]); // filter everything but numbers and letters
    $password = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password"]); // filter everything but numbers and letters

    $sql = mysql_query("SELECT id FROM members WHERE username='$customer' AND password='$password' LIMIT 1"); // query the person
    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = mysql_num_rows($sql); // count the row nums
    if ($existCount == 1) { // evaluate the count
         while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
         }
         $_SESSION["id"] = $id;
         $_SESSION["customer"] = $customer;
         $_SESSION["password"] = $password;
         header("location: index.php");
         exit();
    } else {
        echo 'That information is incorrect, try again <a href="customer_login.php">Click Here</a>';
        exit();
    }
}
?>

I've tried mysql_errno() as you advised.. and it echo 1146

and when I search for that error it says that

1146: Table 'kossu.nonexistenttable' doesn't exist

But I don't know what it means... Please help.

Luke
  • 11,426
  • 43
  • 60
  • 69
iamanapprentice
  • 411
  • 5
  • 19
  • 37
  • 2
    `$sql` is probably false, which is why it is failing. Check the query is correct. – Jake N May 18 '11 at 13:30
  • There might be an error in your sql syntax and mysql_query is returning false. Print out the entire query and paste it into phymyadmin (or similar) and see if it executes correctly. – AllisonC May 18 '11 at 13:31
  • @AllisonC i've try the sql syntax at mysql and it successfully returns the id. – iamanapprentice May 18 '11 at 13:48
  • Read one arbitrary answer below and then use `mysql_error()` to find out, what really happens with your query. There is no need to guess. – KingCrunch May 18 '11 at 13:59

7 Answers7

8

Probably mysql_query() failed. In this case it returns false. Use mysql_error() to find out, what happens.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • hi KingCrunch i've tried to echo mysql_error() as you advice and it echo 1146, when i search for what it means it says that 1146: Table 'kossu.nonexistenttable' doesn't exist, can you help me cause i really have no idea what does this mean... i'v also tried the syntax of $sql to mysql and it successfully flash the column id please help thanx – iamanapprentice May 18 '11 at 14:02
  • 1
    @iamanaprentice: I really don't know, what I should say more. The table `kossu.nonexistenttable` does not exists. I think, its really obvious, what does this mean. In your case it seems, that there is no table `members` to query over. Additional I said `mysql_error()` and not `mysql_errno()`. With the first one you will receive a concrete error message instead of an unspecific error code. This is a little bit more useful than the example output you have taken from the PHP-manual ;) – KingCrunch May 18 '11 at 14:26
  • @ KingCrunch... thank you very much because of mysql_error() i found out the my database were mispelled at my connection code :) thanks again – iamanapprentice May 18 '11 at 16:23
1

When there is an error, mysql_query() returnse false instead of a resource object. Check the return value of mysql_query() before you pass it to mysql_num_rows().

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
0

It's possible you've not selected the right database. I encountered such a problem before and the error was the database I selected.

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
whitehand
  • 75
  • 10
0

When the MySQL query is executed it is expected to return the query result. It returns False if the query was not successfully executed. It seems that your query failed so it returned False. When you try to count rows with mysql_num_rows() to the failed query, it will definitely return error. There might be error in your query. Please check.

0

The cause could be that the SQL query is using an incorrect database. Use the fully-qualified table name, i.e. databaseName.tableName.

In your case:

$sql = mysql_query("SELECT id FROM DBName.members WHERE username='$customer' AND password='$password' LIMIT 1"); 

where 'DBName' is your database name.

MJH
  • 2,301
  • 7
  • 18
  • 20
0

From the docs:

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So, you have an error. Use mysql_error() to find out what.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

mysql_query() failed. It returns false.To see the error use mysql_error()

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Arihant Nahata
  • 1,802
  • 2
  • 19
  • 30