New in PHP 8.2: mysqli_execute_query / mysqli::execute_query

rozhnev

Slava Rozhnev

Posted on January 4, 2023

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

💖 💪 🙅 🚩
rozhnev
Slava Rozhnev

Posted on January 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related