|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
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() |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > What 'IF' condition to use for an empty variable? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|