Page 1 of 1

[SOLVED] Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 11:08 am
by TheBrandon
Hello all,

I run into this a lot in my programming. Doing a Mysql_num_rows to see if there are any result sets but sometimes it just doesn't seem to work.

Here is what I am doing right now:

Code: Select all

    $ticketsql="SELECT * FROM `ticket_sales` WHERE `show_id`=$showid AND `mid`=$userid";
    $ticketresult=mysql_query($ticketsql);
    $tickettotalpull = mysql_num_rows($ticketresult);
 
    /*Create a new entry for them*/
    if (empty($tickettotalpull)){
    echo "empty";
    }else{
    echo "not empty";
    }
If it isn't empty, it executes fine but when it is empty, it doesn't echo anything so my other code doesn't execute.

I've tried this too:

Code: Select all

 
    $ticketsql="SELECT * FROM `ticket_sales` WHERE `show_id`=$showid AND `mid`=$userid";
    $ticketresult=mysql_query($ticketsql);
    $tickettotalpull = mysql_num_rows($ticketresult);
 
    if ($tickettotalpull == 0){
    echo "empty";
    }else{
    echo "not empty";
    }
And I've even tried:

Code: Select all

 
    if ($tickettotalpull !=0){
 
What am I missing or not understanding here? Can anyone help me get this to work?

The code is checking to see if a member has reserved tickets. If they haven't, it executes a INSERT INTO SQL statement. If they have, it executes an UPDATE SQL statement. Right now the update portion works perfectly but I can't get it to test for an empty result set.

I really do run into this a lot so if I could find out the best way to do this kind of check, I would be very grateful.

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 11:28 am
by waylon999
What happens when you print out $tickettotalpull? Does anything show up when there are no rows?

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 11:29 am
by TheBrandon
waylon999 wrote:What happens when you print out $tickettotalpull? Does anything show up when there are no rows?
Nope. It doesn't print anything.

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 11:56 am
by mattpointblank
For doing row counting stuff, I typically do:

Code: Select all

 
 
if(mysql_num_rows($result) < 0)) {
 
//or 
 
if(mysql_num_rows($result) != 0)) {
 
Try those?

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 12:40 pm
by TheBrandon
mattpointblank wrote:For doing row counting stuff, I typically do:

Code: Select all

 
 
if(mysql_num_rows($result) < 0)) {
 
//or 
 
if(mysql_num_rows($result) != 0)) {
 
Try those?
Just tried both, neither work.

Think maybe its my query?

Code: Select all

    $ticketsql="SELECT * FROM `ticket_sales` WHERE `show_id`=$showid AND `mid`=$userid";
    $ticketresult=mysql_query($ticketsql);
    $tickettotalpull = mysql_num_rows($ticketresult);
I am counting results, but maybe there isn't a result at all?

I'm checking basically for ticket_sales where the showid is 1 and the memberid is 1. However if that returns an empty result set, which I expect it to, would num_rows work on it at all?

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 12:42 pm
by TheBrandon
I checked the good ol' manual and it says:

http://us3.php.net/mysql_num_rows
The number of rows in a result set on success, or FALSE on failure.
So basically now, how I check for FALSE?

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 1:04 pm
by waylon999
So basically now, how I check for FALSE?

Code: Select all

 
if(mysql_num_rows($result) == false){
 //code
}
 
if(!mysql_num_rows($result)){
  //code
}
 
I think either of those should work

Re: Help with (if $variable is empty) statement

Posted: Thu Mar 26, 2009 1:07 pm
by TheBrandon
Okay figured it out sorta.

It's not that part that isn't working, it's the first line:

Code: Select all

 
if((int)$totaltixres <= (int)$totalallowed)
{
 
    if(mysql_num_rows($ticketresult) != 0) {
    echo "new insert";
    
    /*Update an existing entry*/
    }else{
    echo "update insert";
    }
    
}else{
/*Error Messages*/
}
Basically each person is allowed to reserve X number of tickets. Thats supposed to check if they have exceeded that or not.