Page 2 of 2

Posted: Wed Apr 11, 2007 11:48 pm
by JellyFish
Everah wrote:Use isset() or empty() to check, don't just throw them into a conditional.
Why? It works just the same...

Btw I've changed a line "$GET['searchQuery']" to "$_GET['searchQuery']", I guess it was more simple then I though. So far it works like a charm. :D

I think I got it the way I want it. If you have any other suggestions to this code you could let me know.

Code: Select all

if ($_GET['searchQuery'] && $_GET['searchField'])
	{
		$searchArray = explode(" ", $_GET['searchQuery']);
		$searchClause = "'".implode("','", $searchArray)."'";
		$query = "SELECT * FROM $table_name WHERE $_GET[searchField] IN($searchClause)";
		echo $query;
	}
	else
	{
		$query = "SELECT * FROM $table_name";
	}

Posted: Thu Apr 12, 2007 12:16 am
by Benjamin
Everah wrote:Use isset() or empty() to check, don't just throw them into a conditional.
As Everah has stated, you need to verify that these variables are set before you run comparisons on them. If you run comparisons on them without them being set, PHP will issue warnings.

Posted: Thu Apr 12, 2007 7:42 pm
by JellyFish
I did that with the first line:

Code: Select all

if ($_GET['searchQuery'] && $_GET['searchField']) 
Wouldn't this do the trick? It works fine when searchQuery equals nothing too. But when it equals " "(spaces) it just doesn't display results(which is fine).

Posted: Thu Apr 12, 2007 9:14 pm
by John Cartwright
Turn on

Code: Select all

error_reporting(E_ALL);
then try running that script with no $_GET parameters.

Essentially, you cannot call a variable that does not exist without a warning. Your code still passes because when they do exist, they would be true.

Posted: Fri Apr 13, 2007 2:13 am
by RobertGonzalez
Think of it in terms of this code:

Code: Select all

<?php
if ($yellowmustard && $purpledentistgloves) {
    // execute
}
?>
If those vars are not defined (null) and PHP attempts to compare them as booleans, well, the PHP engine doesn't like trying to find the value of non-existent variables, do it squaks. You might not see because of the display_errors setting or error_reporting setting, but they are there, and they are having an impact on your script performance.