# Solved: mysql_num_rows problem



## alex_sandro (Aug 19, 2009)

Dear friends, hardly have I managed to deal with one problem using your help and I already have another one.

here is the script, or I'd rather say reduced part of it, as I was trying to find where the problem was.


```
<?php

$host='localhost'; // Host name
$username='username'; // Mysql username
$password='password'; // Mysql password
$db_name='dbname'; // Database name
$tbl_name='admin'; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect to database");
mysql_select_db("$db_name")or die("cannot select the DB");

$myusername=$_POST['Username'];
$mypassword=$_POST['Password'];

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

echo "$count";
?>
```
as I enter the correct values in the form and these values are sent to this script I expect that the value of _$count_ variable must be "1". but I get "0"
Any ideas?


----------



## weedave (Jan 31, 2010)

Why is $count in inverted commas?

echo "$count";

Atm doesn't it just print $count on the screen when it should be printing an integer?
Because its a variable and not a piece of html coding it doesn't have to be in inverted commas, if I remember correctly, long time since I have done PHP.

Or if that is not the case, check your database actaully has a record where your user name and password exist in the same record.

~David


----------



## alex_sandro (Aug 19, 2009)

Inverted commas are not the problem.
the problem, as I have stated, is that I get an integer, but not 1. I get zero. I removed any password encription and everything from the script and menually wrote in the database values for username and password. both are "admin". and when I write them in the form and submit it, I expect "1". 
The aim is to make simple login script and if there is any other way of camparing data received from the form with the data in the database, please let me know


----------



## weedave (Jan 31, 2010)

Try

echo mysql_num_rows($result);

see what it says.


----------



## alex_sandro (Aug 19, 2009)

echo is not the problem. I will remove it in final script.
I want result of counting to be 1 not 0


----------



## weedave (Jan 31, 2010)

Yeah yeah yeah, but echo just lets you see the value of variables or anything really.
So if you "echo mysql_num_rows($result);" you will see directly how many rows are in the $results array rather than putting it in a variable then showing that variable.


----------



## ehymel (Aug 12, 2007)

As with the last answer I suggested to you, I STRONGLY suggest you make use of the built in debugging tools, they are simply invaluable in tracking down problems like this.

This page is a good start: PHP mysql_error()

For this particular case, I would modify your code to incorporate something like this:


```
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if (!$result || $count<1) {
   echo ' ERROR - '.mysql_error();
} else {
   echo "Count = $count";
}
```
This doesn't have to make it to your production code, but during development using the error reporting tools can help you track down problems in your SQL statements, connection problems, logic errors, quotation mark problems, etc. etc.

Hope this helps... I learned the hard way about these built-in tools since I ignored them longer than I should have.


----------



## alex_sandro (Aug 19, 2009)

echo gives me 0 instead of 1. there is no problem with sintax. it checks whether there are such data in the database. it does not give any error. it simply gives 0 while I input correct data.


----------



## ehymel (Aug 12, 2007)

Maybe this is just an artifact of your cut-and-paste, but you are missing a space before both "or die" expressions.

If this is not the problem, then I suggest more error checking to help narrow down the problem. Something like this code:


```
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
```
Notice no double quotes around $host, $username, etc. (not sure this matters). Then modify the code I suggested earlier to spit out the $sql command you sent to the database... you'd be surprised how often the command you *think* you sent does not match what the database *actually* received:


```
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if (!$result || $count<1) {
   echo ' ERROR - $sql= '.$sql.'<br>'.mysql_error();
} else {
   echo "Count = $count";
}
```
Lastly, use something like webmin or phpmyadmin or the command line to look at the database itself and make sure you actually have data that fits what you are looking for.


----------

