Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
If statement not recognising when mysql_num_rows returns 0 results
Old 09-30-2007, 08:22 AM If statement not recognising when mysql_num_rows returns 0 results
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
Here is my code:
PHP Code:
if( mysql_num_rows($result) == ) {
    echo 
"<b>Not Found</b>";

What am I doing wrong here? When the query returns 0 rows, it does not echo "Not Found". I even did an echo mysql_num_rows($result), and it displays 0.

Your help is greatly appreciated.

Frank
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-30-2007, 12:15 PM Re: If statement not recognising when mysql_num_rows returns 0 results
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Try this:
PHP Code:
if(!mysql_num_rows($result) || mysql_num_rows($result) == 0)

    echo 
"<b>Not Found</b>"

Also, you can only use mysql_num_rows for a SELECT query. If you are UPDATE or some other modifier use mysql_affected_rows
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-30-2007, 01:26 PM Re: If statement not recognising when mysql_num_rows returns 0 results
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
No, that didn't work. I am using select, so that is not the issue.
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 09-30-2007, 02:20 PM Re: If statement not recognising when mysql_num_rows returns 0 results
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
The syntax appears to be correct.

Is this the entire code between the php delimiters.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 09-30-2007, 03:00 PM Re: If statement not recognising when mysql_num_rows returns 0 results
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Yeah, I would say you had a problem with $result before the if statement.
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 09-30-2007, 04:40 PM Re: If statement not recognising when mysql_num_rows returns 0 results
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Foundationflash View Post
Yeah, I would say you had a problem with $result before the if statement.
If there were a problem with result then the code I gave him should still work.

Give use a larger portion of your code. I think the problem is elsewhere.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-30-2007, 04:58 PM Re: If statement not recognising when mysql_num_rows returns 0 results
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Are you using any type of output buffering (gzip for example) to supress output to the browser?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 09-30-2007, 05:10 PM Re: If statement not recognising when mysql_num_rows returns 0 results
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
PHP Code:
$dbh=mysql_connect ("localhost""testing""password") or die ('I cannot connect to the database because: ' mysql_error());
mysql_select_db ("testingdb");
$sql "SELECT * FROM testdb WHERE variable = '$variable' LIMIT 1";
$result mysql_query($sql$dbh) or die(mysql_error('reult error'));
while (
$newArray mysql_fetch_array($result)) {
$numrows mysql_num_rows($result);
if(!
mysql_num_rows($result) || mysql_num_rows($result) == 0)
{
    echo 
"<b>Not Found</b>";
echo 
$numrows;
echo 
mysql_error($dbh);

This script outputs 1 if $result returns 1 row, and nothing if the $result has zero lines. I'm just trying to tell the user that the information requested was not found.
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 09-30-2007, 05:16 PM Re: If statement not recognising when mysql_num_rows returns 0 results
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You have it within the result loop, so therefore it will never execute if there are no results returned.

PHP Code:
$dbh mysql_connect("localhost""testing""password") or die('I cannot connect to the database because: ' mysql_error()); 
mysql_select_db("testingdb"); 
$sql "SELECT * FROM testdb WHERE variable = '$variable' LIMIT 1"
$result mysql_query($sql$dbh) or die(mysql_error('reult error')); 
if (
mysql_num_rows($result) < 1)
{
  echo 
"<b>Not Found</b>";
}
else
{
  while (
$newArray mysql_fetch_array($result))
  {
    
// Resutl output goes here...
  
}

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 09-30-2007, 08:38 PM Re: If statement not recognising when mysql_num_rows returns 0 results
Yak Yak Yak Yak Yak

Posts: 593
Location: Rochester, MN
Trades: 0
Alright. Now it's allowing me to display messages based on the presence of absence of a row. However, now it's not passing more than one variable. For example:

$var1 = $newArray[ 'var1' ];
$var2 = $newArray[ 'var2' ];
$var3 = $newArray[ 'var3' ];
$var4 = $newArray[ 'var4' ];
$var5 = $newArray[ 'var5' ];

echo $var1;
echo $var2;
echo $var3;
echo $var4;
echo $var5;

It will only echo the first. Does anyone know why?
__________________

Please login or register to view this content. Registration is FREE
neorunner is offline
Reply With Quote
View Public Profile
 
Old 10-01-2007, 12:53 PM Re: If statement not recognising when mysql_num_rows returns 0 results
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Are you trying to access the variables in the array like an associative array? If you are make sure you add the argument MYSQL_ASSOC to your mysql_fetch_array. ie:

mysql_fetch_array($result, MYSQL_ASSOC)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-21-2011, 09:32 PM Re: If statement not recognising when mysql_num_rows returns 0 results
Junior Talker

Posts: 1
Trades: 0
I was getting the same error but for different reasons.
I had this at first:
$id_num_rows = mysql_num_rows($result) or die(mysql_error($dbc));

removing the " or die(mysql_error($dbc))" made it work correctly for me. When i had the "or die" phrase nothing would be returned. and basically the page stopped being processed. It wasn't so noticeable because I'm using jquery post to just retrieve some data without a page refresh. Maybe your page is just halting without giving an error.

I tried this code in my program and it worked. so maybe the error comes before this

if( mysql_num_rows($result) == 0 ) {
echo
"<b>Not Found</b>";
}
helium3 is offline
Reply With Quote
View Public Profile
 
Old 07-21-2011, 09:41 PM Re: If statement not recognising when mysql_num_rows returns 0 results
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by helium3 View Post
$id_num_rows = mysql_num_rows($result) or die(mysql_error($dbc));

removing the " or die(mysql_error($dbc))" made it work correctly for me. When i had the "or die" phrase nothing would be returned. and basically the page stopped being processed. It wasn't so noticeable because I'm using jquery post to just retrieve some data without a page refresh. Maybe your page is just halting without giving an error.
Yeah, that's not a good way of doing it because if the number of rows returns 0 then it would see it as a false value and trigger the die() method.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to If statement not recognising when mysql_num_rows returns 0 results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.05090 seconds with 12 queries