I am making a search function, and I am not sure how to change this mysqli code to PDO code (Also prevent SQL injection). Hope you guys can help me.
<?php
if(isset($_POST['submit'])) {
$search = $_POST['search'];
$query = "SELECT * FROM posts WHERE post_tags LIKE '%$search%'";
$search_query = mysqli_query($connection, $query);
if(!$search_query) {
die("QUERY FAILED" . mysqli_error($connection));
}
$count = mysqli_num_rows($search_query);
}
?>
Below is my PDO code:
<?php
$search = $_POST['search'];
$search = "%$search%";
$sql = 'SELECT * FROM posts WHERE post_tags LIKE :search';
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':search', $search);
$stmt->execute();
if(!$stmt) {
die("QUERY FAILED" . $pdo->errorInfo());
}
$count = $stmt->fetchColumn();
?>