jayant rawat
jayant rawat

Reputation: 305

MySQL returned an empty result still showing 1 in count

I have a query which is to check data from two different tables everything is working fine though. When i paste the query in cPanel then result is showing like this MySQL returned an empty result set (i.e. zero rows). (Query took 0.0005 sec)

but when i count the query in php it is returing 1 instead of 0 don't know why Here is my Sql query

  SELECT mobile, emailid
FROM tbl_users
WHERE mobile =9653878051
AND emailid = '[email protected]'
GROUP BY mobile
UNION ALL SELECT mobile, email
FROM tbl_addusr
WHERE mobile =9653878051
AND email = 'rawat@gmail
.com'
LIMIT 0 , 30

and i am counting it like this i am storing the sql result in data variable

     $result = mysql_num_rows($data);

  echo count($result)>0?1:0;  

and it is resulting 1 instead of 0

Upvotes: 0

Views: 831

Answers (4)

Abhijit Jagtap
Abhijit Jagtap

Reputation: 2702

mysql_num_rows — Get number of rows in result not array

Description

int mysql_num_rows ( resource $result ) Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

Parameters

result The result resource that is being evaluated. This result comes from a call to mysql_query().

Return Values

The number of rows in a result set on success or FALSE on failure.

    <?php

    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("database", $link);

    $result = mysql_query("  SELECT mobile, emailid
FROM tbl_users
WHERE mobile =9653878051
AND emailid = '[email protected]'
GROUP BY mobile
UNION ALL SELECT mobile, email
FROM tbl_addusr
WHERE mobile =9653878051
AND email = 'rawat@gmail
.com'
LIMIT 0 , 30", $link);
    $num_rows = mysql_num_rows($result);

    echo "$num_rows Rows\n";
    echo $num_rows>0?1:0;//compare like this

    ?>

check doc http://php.net/manual/en/function.mysql-num-rows.php and http://php.net/manual/en/function.count.php

Upvotes: 0

moni_dragu
moni_dragu

Reputation: 1163

You do not need to count anything. mysql_num_rows gives you the number of rows.

Use like this:

$result = mysql_query($query);
echo  mysql_num_rows($result);

Upvotes: 1

Rax Weber
Rax Weber

Reputation: 3780

You are using the count() function of PHP, which from the manual states:

Returns the number of elements in array_or_countable. If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.

So on your case, it will return 1. Just remove the count() function and compare the value of $result itself in your ternary operator.

Upvotes: 0

Twinfriends
Twinfriends

Reputation: 1987

1: Stop using mysql_* functions. Use mysqli or PDO instead.

2: Use SQL Count: http://www.w3schools.com/sql/sql_func_count.asp

Oh yes, just as advise: don't use variable names like $aaaaaa ... names like this one just say nothing about what the variable is and when you repopen the project in a few month you won't understand anything what you've done.

Upvotes: 0

Related Questions