0

I am noob in php and I have spent many hours trying to understand why my else block is not getting called. I have tried number of things but just not sure why it is not getting called. I am sure its something stupid i am doing.

     <?php   
 $base_url = 'http://localhost/mine/';
     $per_page = 3;                           //number of results to shown per page 
     $num_links = 8;   
     $cur_page = 1;                          // set default current page to 1

if(isset($_REQUEST['string'])<>'') 
{
      $search_string = " title LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR description LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'";
      $query = mysql_query("SELECT * FROM products WHERE ".$search_string);
      $row = mysql_num_rows($query) or die(mysql_error());

      $row = $row; 

      $cur_page = $_GET['page'];
      $cur_page = ($cur_page < 1)? 1 : $cur_page; 
      $offset = ($cur_page-1)*$per_page;                //setting offset

      $pages = ceil($row/$per_page);              // no of page to be created


      $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
      $end   = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;

      $res  =   mysql_query("SELECT * FROM products WHERE ".$search_string." ORDER BY title LIMIT ".$per_page." OFFSET ".$offset);  

      while($row=mysql_fetch_array($res))
         {
         include ('form.php');
         }
    }
      else
     include ('alert.php');

    ?>

i forgot to add that i am using the get method for the url's . So i think it might be possible that even when there is no search term it is still showing index.php?string= which would mean that it is still taking the if statement as true and not going to the else statement. Please correct me if i am wrong.

here is a snippet from my pagination.php

if (strstr($_SERVER['REQUEST_URI'], "?string="))
        {
            echo '<span id="prev"> <a href="' . $_SERVER['PHP_SELF'] . '?string=' . $_GET['string'] . '&page=' . (1) . '">' . $dir . '</a> </span>';
        }
        else
            echo '<span id="prev"> <a href="' . $_SERVER['PHP_SELF'] . '?page=' . (1) . '">' . $dir . '</a> </span>';
9
  • Try to add curly braces and if it doesn't works well add echo for debugging step by step. Commented Dec 16, 2013 at 21:00
  • 1
    <> is SQL syntax. The PHP equivalent would be != therefore your if(isset($_REQUEST['string'])<>'') should be if(isset($_REQUEST['string'])!='') Commented Dec 16, 2013 at 21:02
  • curly braces is not necessary when is just one statement after the if/else @user602525, ChoiZ Commented Dec 16, 2013 at 21:02
  • <> works perfectly fine in PHP. <> and != are actually 100% interchangeable. That comment is invalid. Commented Dec 16, 2013 at 21:11
  • 1
    The problem is that "isset(xyz) <> ''" is ALWAYS TRUE. POSSIBLE SOLUTION: if (!isset(xyz) && xyz <> ''). BETTER SOLUTION: if (!empty(xyz)). Commented Dec 16, 2013 at 21:30

7 Answers 7

2

Your IF-condition is wrong

isset returns true or false it does not return a value that could be compare to empty or what is between your quotes

if(!empty($_REQUEST['string'])) 

The above if statement should be enough to check if the value is set and if it's not empty\

Some things you should read

isset() vs empty() vs is_null()

empty()'s documentation

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

Comments

2

OP's condition could also be written as if(!isset($_REQUEST['string']))


EDIT : I was wrong earlier about:

<> is SQL syntax

The PHP equivalent would be != therefore your if(isset($_REQUEST['string'])<>'') should be if(isset($_REQUEST['string'])!='')

8 Comments

$a <> $b Not equal TRUE if $a is not equal to $b after type juggling. is valid php.net/manual/en/language.operators.comparison.php for more info
<> you can perfectly use that operator, it's not only SQL Syntax. PHP uses it too.
Yes, I just noticed that now. @ChosenWann
Which could also be written as if(!isset($_REQUEST['string']))
@Fred-ii- Exactly, but I think the OP also wanted to check whether it was empty or not, so using empty only is sufficient for what the user needs.
|
0

Use

if(isset($_REQUEST['string'])) { /*...*/ } else { /*...*/ }

or

if($_REQUEST['string'] != '') { /*...*/ } else { /*....*/ } (while the latter probably throwing a warning in case of the parameter string was not passed at all)

Not both at the same time like you did.

Comments

0

Try using this as the condition for the IF:

if(isset($_REQUEST['string']) && trim($_REQUEST['string'])!=''){ ... 

First it will check if the variable exists, and if it does, it will check if it has some text in it - just in case

Comments

0

To understand why it's not working, we need to break it apart into parts.

First, you are comparing the value of isset($_REQUEST['string']) (which will always return a boolean) to an empty string.

If you do

var_dump(isset($_REQUEST['string']));

You should see either bool(true) or bool(false) as the output depending on if it's set.

However when you compare a boolean to a string, it will be type cast the boolean into a string so that both types are the same.

var_dump( (string) false ); // Outputs string(0) ""
var_dump( (string) true ); // Outputs string(1) "1"

So you are essentially doing the following:

// When $_REQUEST['string'] is unset
if ("" <> "") { ... } // Will always be false because "" === ""

// When $_REQUEST['string'] is set
if ("1" <> "") { ... } // Will always be *true* because "1" != ""

So therefore, the reason must be that $_REQUEST['string'] is, in fact, unset. I have tested this myself in PHP 5.5.3:

$set_var = 'i_am_set';
var_dump( isset($set_var) <> '' ); // Outputs bool(true)
var_dump(  isset($unset_var) <> '' ); // Outputs bool(false)

However, I think that what you're looking for is empty() which will return false if the variable is either unset or empty.

Comments

0

Could be in this way, your say you'w using GET method

$string = isset($_GET['string']) ? $_GET['string'] : NULL;

if(!empty($string ){
   //your stuff
}

Side Note: read this link How can I prevent SQL injection in PHP?

1 Comment

You're mixing up []s and ()s and also didn't put a $ in front of string.
-2

isset() has a boolean return value.

See http://www.php.net/isset.

You should just be able to use it in the if clause.

if(isset($_REQUEST['string'])) 
{

}
else
{

}

You can also use array_key_exists(). See http://www.php.net/function.array-key-exists. This doesn't actually check to make sure that the request parameter has a useful value.

if(array_key_exists('string', $_REQUEST)) 
{

}
else
{

}

empty() will check for an array key, null, and no value. That's what I'd use.

if(!empty($_REQUEST['string'])) 
{

}
else
{

}

2 Comments

This only fulfills the user's first condition which is checking if $_REQUEST['string'] is set or not, it does not fulfill his second condition.
I assume you mean, if the request parameter has any value. You're probably right empty() is what's called for here.

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.