4 Replies - 585 Views - Last Post: 20 March 2018 - 12:31 AM Rate Topic: -----

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Add wildcard to LIKE statement using PDO prepared statement bind param

Posted 17 March 2018 - 11:59 AM

Update: It works with bindValue instead of bindParam:
$stmt->bindValue(':search_box', '%' . $search_text . '%');


------------------------------------------------------------------------------------

How can I change the following:
WHERE search_results LIKE :search_text";

to work with wildcard? I tried some testing with some numbers, so I inserted to the 'search_results' column the number '12345', and the query returns result only if I search for the exact number, but if I type '123' it doesn't work.

The following attempts failed:
Attempt #1:
$search_text = $_POST['search_text']; // data from search form
$search_text = "%$search_text %"; //doesn't return any result



Attempt #2:
WHERE search_results LIKE %:search_text%"; // Fatal error


Thanks

This post has been edited by Foobarer: 17 March 2018 - 12:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Add wildcard to LIKE statement using PDO prepared statement bind param

#2 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: Add wildcard to LIKE statement using PDO prepared statement bind param

Posted 17 March 2018 - 12:34 PM

Update: It works with bindValue instead of bindParam:
$stmt->bindValue(':search_box', '%' . $search_text . '%');

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: Add wildcard to LIKE statement using PDO prepared statement bind param

Posted 17 March 2018 - 01:37 PM

In your earlier example you had an extra space between the parameter value and the second percent sign.



Note that it would have been clearer if you had just added your solution as a second post rather than modifying the original question.
Was This Post Helpful? 1
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4223
  • View blog
  • Posts: 13,380
  • Joined: 08-June 10

Re: Add wildcard to LIKE statement using PDO prepared statement bind param

Posted 19 March 2018 - 01:26 AM

Quote

It works with bindValue instead of bindParam

That's because bindParam() expects a variable reference to be given, and not a value.

Note: the Param in bindParam() does not relate to the SQL bound parameters, but the PHP (function) parameters.
Was This Post Helpful? 1
  • +
  • -

#5 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: Add wildcard to LIKE statement using PDO prepared statement bind param

Posted 20 March 2018 - 12:31 AM

thanks ! :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1