PDO Prepared Statement problem

Associate
Joined
31 Dec 2002
Posts
458
Hi,

I have a weird problem. I am using PDO prepared statements against an MySQL database. When I use named parameters the results return fine, however I have one condition that if the string "all" is passed in as the GET value then the prepared statement should select all from the table. This is the only condition not working. All of the named parameter statements are fine.

Code:
if($location && !$name)
{
	if ($location == "all")
	{
		$stmt = $connection->prepare("SELECT * FROM shops");
		$stmt->execute();

	}

	else
	{
		$stmt = $connection->prepare("SELECT * FROM shops WHERE location LIKE :location");
		$locationParam = "%".$location."%";
		$stmt->bindParam(':location', $locationParam);
		$stmt->execute();

	}
	
}

Looking forward to any replies. My connection page has the following:

Code:
	try 
	{
	$connection = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbusername, $dbpassword);
	$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
	}

	catch(PDOException $ex) 
	{
    echo "There was a problem connecting!";
    echo $ex->getMessage();
	}

I am also using Ajax to send and receive the requests using the Prototype library. I have a simple search box and a select list. The select list filters on shop name or location and the searchbox value is the parameter passed into the Ajax GET request. The results are passed back as XML which is generated by a loop.

Code:
$stmt->setFetchMode(PDO::FETCH_ASSOC);

echo "<shops>";

while($row = $stmt->fetch())
{
	echo "<shop>";
	echo "<name>"  . $row["name"] . "</name>";
	echo "<location>"  . $row["location"] . "</location>";
	echo "<category>" . $row["category"] . "</category>";
	echo "<telephone>"  . $row["telephone"] . "</telephone>";
	echo "<email>"  . $row["email"] . "</email>";
	echo "</shop>";
}

echo "</shops>";
 
Last edited:
Associate
OP
Joined
31 Dec 2002
Posts
458
Hi, I have solved the problem. I forgot to check the server request method. I enclosed everything in:

Code:
if ($_SERVER["REQUEST_METHOD"]=="GET")
{

}

and it works fine now. Funny how the other queries worked without it though!!!!
 
Back
Top Bottom