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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...