Jump to content

PDO binding for a MYSQL LIKE


niche

Recommended Posts

This code works, but can someone tell me how I can use an array instead of bindParam in this situation. 

try {	
			$sql = '
				SELECT 
					*
				FROM 
					nconnectz
				WHERE
					itemandsize LIKE :itemandsize
				AND 
					price > 0	
				ORDER BY
					price ASC				
			';
			$stmt2 = $db2->prepare($sql);
		} catch (PDOException $e) {
			echo $e->getMessage() . '<br>';
		}	
				
		$var = "%Flour%";
		
		try {	
			$stmt2->bindParam(':itemandsize',$var);
			$stmt2->execute();
		} catch (PDOException $e) {
			echo $e->getMessage() . '<br>';
		}	

Something like?

$stmt2->execute(array(
:itemandsize => '%Flour%' ));

 

Edited by niche
Link to comment
Share on other sites

Interesting.

When I strip-out all the white space it works:

$stmt2->execute(array(':itemandsize' => '%' . $var . '%'));

I've heard of white space being a problem, but haven't knowingly experienced it.

Any thoughts?  Urban legend?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...