MySQL query rows that contain...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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";
	}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply