Getting MySQL message. Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

I'm trying to execute the following code:

$searchString = "john";

$sql = " select custid, firstname, lastname, homephone, cellphone, workphone, otherphone " .
       " from customers " .
       " where (firstname like ? or lastname like ?) " .
       " and deleted = 'n' " .
       " order by lastname, firstname " .
       " limit 10 offset 0";

$db= DB::getPDOConnection();
$stmt = $db->prepare($sql);
/*********************************************************
CAN SOMEONE PLEASE TELL ME WHAT I'm DOING WRONG IN THE 
WAY I'M BOUNDING MY ANONYMOUS PARAMETERS? I KEEP GETTING
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: no parameters were bound
AND I DON'T GET MY RESULT SET.
**********************************************************/
$stmt->bindValue(1, '%' . $searchString . '%', PDO::PARAM_STR);
$stmt->bindValue(2, '%' . $searchString . '%', PDO::PARAM_STR);
$stmt = $db->prepare($sql);
$stmt->execute();

Open in new window


I'm not seeing what I'm doing wrong. Can someone please tell me why the above keeps failing?

Thanks.
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Why are you running prepare twice (20) especially after you bind values in lines 18,19
That is then assigned to $stmn which means when you run execute, the values are not bound
Remove the prepare on line 19 leaving the prepare only on line 11.
$stmn->execute("'%$searchstring%'","'%$searchstring%'");
And see if
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
Arnold, you're right, I had two prepare statements, lol. This happens towards the end of the day when I'm so tired. I took the duplicate prepare statement and that was it.

Thanks for being my second pair of eyes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.