Dual procedural and object-oriented interface

The mysqli extension features a dual interface. It supports the procedural and object-oriented programming paradigm.

Users migrating from the old mysql extension may prefer the procedural interface. The procedural interface is similar to that of the old mysql extension. In many cases, the function names differ only by prefix. Some mysqli functions take a connection handle as their first argument, whereas matching functions in the old mysql interface take it as an optional last argument.

Example #1 Easy migration from the old mysql extension

<?php
$mysqli 
mysqli_connect("example.com""user""password""database");
$result mysqli_query($mysqli"SELECT 'Please do not use the deprecated mysql extension for new development. ' AS _msg FROM DUAL");
$row mysqli_fetch_assoc($result);
echo 
$row['_msg'];

$mysql mysql_connect("example.com""user""password");
mysql_select_db("test");
$result mysql_query("SELECT 'Use the mysqli extension instead.' AS _msg FROM DUAL"$mysql);
$row mysql_fetch_assoc($result);
echo 
$row['_msg'];

The above example will output:

Please do not use the deprecated mysql extension for new development. Use the mysqli extension instead.

The object-oriented interface

In addition to the classical procedural interface, users can choose to use the object-oriented interface. The documentation is organized using the object-oriented interface. The object-oriented interface shows functions grouped by their purpose, making it easier to get started. The reference section gives examples for both syntax variants.

There are no significant performance differences between the two interfaces. Users can base their choice on personal preference.

Example #2 Object-oriented and procedural interface

<?php

$mysqli 
mysqli_connect("example.com""user""password""database");

$result mysqli_query($mysqli"SELECT 'A world full of ' AS _msg FROM DUAL");
$row mysqli_fetch_assoc($result);
echo 
$row['_msg'];

$mysqli = new mysqli("example.com""user""password""database");

$result $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row $result->fetch_assoc();
echo 
$row['_msg'];

The above example will output:

A world full of choices to please everybody.

The object-oriented interface is used for the quickstart because the reference section is organized that way.

Mixing styles

It is possible to switch between styles at any time. Mixing both styles is not recommended for code clarity and coding style reasons.

Example #3 Bad coding style

<?php

$mysqli 
= new mysqli("example.com""user""password""database");

$result mysqli_query($mysqli"SELECT 'Possible but bad style.' AS _msg FROM DUAL");

if (
$row $result->fetch_assoc()) {
    echo 
$row['_msg'];
}

The above example will output:

Possible but bad style.

See also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial