In continuation of my previous article, I want to write about another nice innovation in the mysqli
module.
Since PHP 8.2.0 there is a new function mysqli_execute_query
or mysqli::execute_query
if you prefer object-oriented style.
This is a really nice feature. It allows you to kill two (three) birds with one stone:
- generate a prepared statement
- execute it by substituting values from the array of variables
- retrieve query result as associative array
Let's try this in practice:
<?php
$query = 'SELECT Name FROM City WHERE District=? ORDER BY Name LIMIT 5';
/* here the magic */
$result = $mysqli->execute_query($query, ['Nordrhein-Westfalen']);
foreach ($result as $row) {
printf("%s \n", $row["Name"]);
}
You can try this code here: phpize.online
Top comments (1)
This is a very useful article.
Thank you Slava