mysql_numrows

mysql_numrows

am 12.10.2008 20:19:56 von Ron Piggott

When I do the following command:

$number_of_entries=mysql_numrows($blog_activity_result);

and there are no rows I get an error (Warning: mysql_numrows(): supplied
argument is not a valid MySQL result resource in ...)

When that happens I want to display a greeting to invite the user to
engage in the community blog I am creating. How can I make my own
greeting, instead of displaying the error mySQL is giving me?

Ron


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: mysql_numrows

am 12.10.2008 20:59:11 von legnakarlos

You can use, $number_of_entries=3D@mysql_numrows($blog_activity_result);

with @ you silence the error in the display.

Robert C.


El 12/10/2008, a las 01:19 p.m., Ron Piggott escribi=F3:

> When I do the following command:
>
> $number_of_entries=3Dmysql_numrows($blog_activity_result);
>
> and there are no rows I get an error (Warning: mysql_numrows(): =20
> supplied
> argument is not a valid MySQL result resource in ...)
>
> When that happens I want to display a greeting to invite the user to
> engage in the community blog I am creating. How can I make my own
> greeting, instead of displaying the error mySQL is giving me?
>
> Ron
>
>
> --=20
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: mysql_numrows

am 12.10.2008 23:54:54 von dmagick

Ron Piggott wrote:
> When I do the following command:
>
> $number_of_entries=mysql_numrows($blog_activity_result);
>
> and there are no rows I get an error (Warning: mysql_numrows(): supplied
> argument is not a valid MySQL result resource in ...)

Fix the error from $blog_activity_result as someone else mentioned.

If there are no rows returned from a query, mysql_numrows will return 0
- it will only show an error if you give it a bad link identifier.

eg

$query = "select * from table_that_doesnt_exist limit 1";
$result = mysql_query($query);
$rows = mysql_numrows($result); <-- will give an error

$query = "select * from table_that_is_empty limit 1";
$result = mysql_query($query);
$rows = mysql_numrows($result); <-- will return 0

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: mysql_numrows

am 14.10.2008 17:00:18 von Bastien Koert

------=_Part_7916_12994979.1223996418084
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Sun, Oct 12, 2008 at 5:54 PM, Chris wrote:

> Ron Piggott wrote:
>
>> When I do the following command:
>>
>> $number_of_entries=mysql_numrows($blog_activity_result);
>>
>> and there are no rows I get an error (Warning: mysql_numrows(): supplied
>> argument is not a valid MySQL result resource in ...)
>>
>
> Fix the error from $blog_activity_result as someone else mentioned.
>
> If there are no rows returned from a query, mysql_numrows will return 0 -
> it will only show an error if you give it a bad link identifier.
>
> eg
>
> $query = "select * from table_that_doesnt_exist limit 1";
> $result = mysql_query($query);
> $rows = mysql_numrows($result); <-- will give an error
>
> $query = "select * from table_that_is_empty limit 1";
> $result = mysql_query($query);
> $rows = mysql_numrows($result); <-- will return 0
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
The best bet is to handle the errors properly


$num_rows = -1;
if (!$result || mysql_err_no($result) != 0)
{
$num_rows = mysql_num_rows($result);
}

if ($num_rows >-1){
//do something here

}

--

Bastien

Cat, the other other white meat

------=_Part_7916_12994979.1223996418084--

Re: mysql_numrows

am 14.10.2008 23:47:31 von dmagick

> $num_rows = -1;
> if (!$result || mysql_err_no($result) != 0)
> {
> $num_rows = mysql_num_rows($result);
> }
>
> if ($num_rows >-1){
> //do something here
>
> }

Don't you have that backwards?

If there is a result && there is no error, do the num_rows.

if ($result && mysql_err_no($result) == 0) {
$num_rows = mysql_num_rows($result);
}

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php