PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 26th, 2005, 04:43 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
What 'IF' condition to use for an empty variable?

Could anybody tell me what condition I can use in a IF ELSE statement. For example, after connecting to the database a query is done. This sets up the variable $result if the query has matched fields with search parameters.

$result=mysql_query(SELECT * FROM table WHERE field1 like $search)
while($r=mysql_fetch_array($result)){
if ($results condition here ==false or empty){
echo "no data"
}
else
{

echo fields blah blah
}

maybe something like !isset($result); or empty($result); used here for this 'empty' variable condition, but how I don't know.

Reply With Quote
  #2  
Old January 27th, 2005, 06:23 AM
daidalus13 daidalus13 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 daidalus13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 51 sec
Reputation Power: 0
have a look at this

Have a look at this

$result=mysql_query(SELECT * FROM table WHERE field1 like $search)

# I always check if the query was executed succesfully
# The following is quoted from the php manual:
# "mysql_query() returns a resource identifier or FALSE if the query was not executed correctly"

if(!$result) die('Database Error. Please try again later'.mysql_error());

# if reached here, your query was valid

# Now, about your problem
# The test of the while loop will not succeed if there are no rows left in the result
# and will never succeed if the result is completely empty
# Quoted again from the php manual:
# "array mysql_fetch_array ( resource result [, int result_type])
# Returns an array that corresponds to the fetched row, or FALSE if there are no more rows."
# So, you do not need the if statement in your while loop

while($r=mysql_fetch_array($result))
{
# do something with the data of this row
}

# if you really want to have a message when the result has zero rows you can try this

if (!mysql_num_rows($result))
{
echo "no data";
}

I hope it helps
daidalus13

Reply With Quote
  #3  
Old January 27th, 2005, 07:02 AM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
Thankyou daidalus13
Yes it did help!
I have been gradually realising that I might putting the IF loop in the wrong place but was not sure until you posted your reply. I've been putting lots of echo 'isset($result): '.isset ($result).'<br/>'; everywhere in the the code, just to see any pattern with 1's and blanks. Your solution is simple and perfect, thanks again.

Reply With Quote
  #4  
Old January 27th, 2005, 07:31 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
You can shorten this:

$result=mysql_query(SELECT * FROM table WHERE field1 like $search)
if(!$result) die('Database Error. Please try again later'.mysql_error());

to simple this:
$result=mysql_query(SELECT * FROM table WHERE field1 like $search) or die('Database Error. Please try again later'.mysql_error());

To answer the question "what if condition to use for an empty variable), I do use isset() often, but usually for Query String or POSt variables.

I rarely test if $result is set, since as daidalus13 points out, if there was an error in your query that die() statement will execute. Returning no rows is different then an error, in which case utilize the mysql_num_rows()

Reply With Quote
  #5  
Old January 27th, 2005, 01:38 PM
SnapCracker's Avatar
SnapCracker SnapCracker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Kent, United Kingdom
Posts: 165 SnapCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 21 h 22 m 31 sec
Reputation Power: 4
Thanks MadCowDzz

for the info, however testing the state of $result was necessary as I am doing a multiple search of my database using the 'foreach' command from posted results from 2 sets of checkboxes. I have posted a question before "To display fields using php multiple selection criteria on MySql database" but I think it might have been too much like 'War and peace'! So I have decided to break it up into small chunks. So at the moment I have sucessfully got it whizzing around loops querying the table with all combinations of the 2 posted arrays and displaying the results when there was a match. That is why I wanted it to show "no records found" in the case where there were no matchs with particular combinations along side the details of records where they did.
However, since then I've changed my mind, and used a modified daidalus13 'IF' statement to prevent a separating banner which shows the 2 search variables being displayed when there no records. Thanks for your interest and input.

Reply With Quote
  #6  
Old January 27th, 2005, 02:06 PM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
Just for the record -- be careful using isset() to check variables. In the above '$result = msyql_query(etc.)', isset($result) will always return TRUE, because whether the query fails, returns records, or returns no records, the $result variable is still being set to something, either the boolean FALSE or a resource id.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > What 'IF' condition to use for an empty variable?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT