Click to See Complete Forum and Search --> : Quering MySQL DB with SELECT COUNT in PHP


GUIR
11-26-2004, 07:46 AM
I have used the following SQL query using PHP to display "User ID already registered" when a new visitor to my site tries to registered with existing UserId which has been registered to anyone else.

//Code to connect DB

$sql = "SELECT count(userid) FROM customer WHERE userID = \"GUIR\"";
print mysql_query($sql);

//

If value exists it should show "1 or more".
If not it should show "0".

I tried with this query using PHPMyAdmin. It works fine.

But when used with PHP as above it gives the output jargon : "Resource id #4", irrespective of such userID exists or not.

The output given by PHP is always "Resource id#4"; But the exact value must be 0, 1 or more.

When the same query is used with different userID the output is "Resource id#5";.

I've no idea what are these "Resource id"s.
I think therers no wrong with the sql query.
PHP is doing some (I dono);

Can someone pls explain me what is happening and/or are there any alternative Queries to do the same.

Again : The above query works fine with PHPMyAdmin.

Regards.

DJsAC
11-26-2004, 08:48 AM
With the php code your using, all you're saying is that you want the script to look that up in the database, and then you're asking it what it's using to refer to those values.

The Reference id# is the type of string/value/stream it's using to keep track of the data.

To get the number of rows, use something along the lines of
mysql_num_rows() :D


Or something like this:

$sql = mysql_query("SELECT count(userid) FROM customer WHERE userID = \"GUIR\"") or die(mysql_error());
$results = mysql_result($sql, "0");
print($results);

GUIR
11-26-2004, 10:36 AM
Hello!

Thats What I exactly wanted.......

ThanX V much for your help.........

Visit Sri Lanka...

Regards....