DEV Community

Slava Rozhnev
Slava Rozhnev

Posted on

3 1

PHP 8.1: MySQLi: Bind in Execute

PHP have two libraries are mainly used to interact with the MySQL database:
PDO (PHP Data Objects) - a universal driver that allows you to work with various databases, including MySQL
MySQLi (MySQL improved) is an extension of the relational database driver used in the PHP programming language to provide access to MySQL databases.

Although MySQLi shows slightly better performance with prepared statements, in most cases I chose PDO because of its convenient binding syntax.

Just compare:

$mysqli_statement = $mysqli->prepare('SELECT * FROM posts WHERE id = ? and author = ?');
$mysqli_statement->bind_param('ds', $postId, $author);
$mysqli_statement->execute();
Enter fullscreen mode Exit fullscreen mode

Test above code on PHPize.online

With concise PDO:

$pdo_statement = $pdo->prepare('SELECT * FROM posts WHERE id = ? and author = ?');
$pdo_statement->execute([$postId, $author]);
Enter fullscreen mode Exit fullscreen mode

Run code here

However, since PHP 8.1, thanks to an improvement suggested by Kamil Tekiela
everything has become much simpler and the syntax of MySQLi has become as pleasant as in PDO:

$mysqli_statement = $mysqli->prepare('SELECT * FROM posts WHERE id = ? and author = ?');
$mysqli_statement->execute([$postId, $author]);
Enter fullscreen mode Exit fullscreen mode

https://phpize.online/s/DU

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more