1

In my code I am checking whether variables exist. But it is not turning out the way I expect it.

This is what I'm trying to do:

  1. Retrieve $result from database.
  2. Execute a block of code only if a result has been extracted (otherwise do nothing/skip over/move forward).

I used if(isset($result)) but it doesn't seem to be working, looks like the if is being executed, even though it shouldn't when there is no data in $result. (but with problems because there is no actual $result variable to use in this block)

5
  • 1
    Can you post the code you use to do this as of now? It'll be much easier to point out the exact issue ... Commented Jan 2, 2012 at 17:56
  • Have you tried going with if(!is_null($result)) ? Commented Jan 2, 2012 at 17:56
  • Post the piece of code where $result is supposed to be initialized Commented Jan 2, 2012 at 18:03
  • I was checking the if on the mysql_query and now moved it to the mysql_fetch_assoc, and changed it to if($result) and now it is working. Thanks! Commented Jan 2, 2012 at 19:07
  • @omg ponies this happens to be a mysql question too, the answer actually was related to the result/s from a mysql query Commented Jan 2, 2012 at 21:30

5 Answers 5

3

Doesn't just if($result) work for you? If you have error reporting showing E_NOTICEs, then try if(isset($result) && $result)

Sign up to request clarification or add additional context in comments.

1 Comment

I think this would depend on the query function used ... mysql_query() would return FALSE on failure, but others may not ...
2

Depending on how you are executing your query, there's a possibility you should be using empty() in place of isset().

From the documentation:

isset
Determine if a variable is set and is not NULL.

empty
Determine whether a variable is considered to be empty.

For example:

$var = '';

isset($var) would return true as it has been set, but empty($var) would return false because its value is an empty string.

Comments

2

Isset only works for non instantiated variables. When you set $result to the db response you are actually setting the variable.

You could instead use mysql_fetch_assoc ( resource $result ) or mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] ) depending on what you want to do. Both of those functions will return false if there is not a response.

http://www.php.net/manual/en/function.mysql-fetch-assoc.php http://www.php.net/manual/en/function.mysql-fetch-array.php

Comments

1

To check if there is any data extracted, use this :

$connect = mysql_connect("127.0.0.1",$sql_username,$sql_password);
$data = mysql_query($sql_query,$connect);
$result = mysql_fetch_array($data);
if($result) { ... }

1 Comment

Thanks! I used the if on the $data, moved it down to the $result, and it's working now. Thanks!!
1

If you use something like mysql_query it will return a resource, or false on error. That means it will always be set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.