DEV Community

Cover image for New in PHP 8.2: mysqli_execute_query / mysqli::execute_query
Slava Rozhnev
Slava Rozhnev

Posted on

New in PHP 8.2: mysqli_execute_query / mysqli::execute_query

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"]);
}
Enter fullscreen mode Exit fullscreen mode

You can try this code here: phpize.online

Top comments (1)

Collapse
 
amirhorev profile image
Amir Horev

This is a very useful article.
Thank you Slava