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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

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

Post 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.
Last edited by TheBrandon on Thu Mar 26, 2009 3:53 pm, edited 1 time in total.
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

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

Post by waylon999 »

What happens when you print out $tickettotalpull? Does anything show up when there are no rows?
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

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

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

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

Post 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?
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

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

Post 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?
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

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

Post 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?
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

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

Post 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
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

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

Post 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.
Post Reply