MySQL with php LIKE and PDO

0

Hi, I hope you are very well, this is my first introduction with PDO and it made me very interesting, as a result of this I have had some doubts about the SELECT that I hope you can help me at the moment I have this like this:

SELECT id FROM tabla WHERE campo LIKE ?

or

SELECT id FROM tabla WHERE campo LIKE ?​ OR campo2 LIKE CONTACT ('%',:buscar,'%')

or

'SELECT id FROM tabla WHERE campo LIKE %?%'  (o que pasa si aveces quiero consultar dos campos, tres o según decida el usuario, desde el formulario de búsqueda)​

and I have seen many variants on the Internet but nothing concise I would like to know your opinion or the best way to make such a query.

    
asked by Vanss Ket Ball 12.11.2017 в 18:06
source

1 answer

0

Hello, I explain to you based on the official MySQL site, everything depends on your need.

These are examples to a table of (pets)

With this query you can find the names (name) that begin with the letter b.

SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
  

Update

Check in PDO:

$query = $db->prepare('SELECT * FROM pet WHERE column LIKE ?');
$query->bindValue(1, "%fy", PDO::PARAM_STR);
$query->execute();

With the following query you can search with the names that end with the letter or word fy

SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

The following query is to find the names that contain the letter W somewhere:

SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

To find names that contain exactly an exact number of characters, use instances of the _ character pattern, for example having only 5 characters:

SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

As I told you everything depends on your need, however here I leave the official documentation:

Link: Matching Patterns

    
answered by 12.11.2017 / 18:14
source