9951 explained code solutions for 126 technologies


php-pdoSelect LIKE query example with PHP PDO


$st = $pdo->prepare('SELECT * FROM test WHERE name LIKE :name');
$st->execute([':name' => '%A%']);
foreach ( $st as $row ) {
  print_r($row);
}ctrl + c
$pdo->prepare

prepare given query to execute

$st->execute(

run query on the server

LIKE :name

use standard named placeholder in LIKE query

%A%

wrap searched value around with % or _ depending on your goals


Usage example

<?php

$pdo = new PDO('mysql:host=localhost;dbname=test', 'usr', 'pwd');

$st = $pdo->prepare('SELECT * FROM test WHERE name LIKE :name');
$st->execute([':name' => '%A%']);

foreach ( $st as $row ) {
  print_r($row);
}
output
Array
(
    [id] => 1
    [0] => 1
    [age] => 25
    [1] => 25
    [name] => A
    [2] => A
)