Jump to content
Sign in to follow this  
niche

PDO binding for a MYSQL LIKE

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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

There's no problem with whitespace in the code itself. What I do find is that in your first post, you forgot to quote :itemandsize string.

Share this post


Link to post
Share on other sites

Voila!  Thanks for your help.

Share this post


Link to post
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
Sign in to follow this  

×