pdo get result code example

Example 1: pdo db connection

<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
?>

Example 2: pdo php search table

$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
	$search_keyword = $_POST['search']['keyword'];
}
$sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC ';
...
...
$pdo_statement = $pdo_conn->prepare($query);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();

Tags:

Php Example