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");
$res mysqli_query($mysqli"SELECT 'Please, do not use ' AS _msg FROM DUAL");
$row mysqli_fetch_assoc($res);
echo 
$row['_msg'];

$mysql mysql_connect("localhost""root""");
mysql_select_db("test");
$res mysql_query("SELECT 'the mysql extension for new developments.' AS _msg FROM DUAL"$mysql);
$row mysql_fetch_assoc($res);
echo 
$row['_msg'];
?>

Exemplul de mai sus va afișa:

Please, do not use the mysql extension for new developments.

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");
if (
mysqli_connect_errno($mysqli)) {
    echo 
"Failed to connect to MySQL: " mysqli_connect_error();
}

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

$mysqli = new mysqli("example.com""user""password""database");
if (
$mysqli->connect_errno) {
    echo 
"Failed to connect to MySQL: " $mysqli->connect_error;
}

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

Exemplul de mai sus va afișa:

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");
if (
$mysqli->connect_errno) {
    echo 
"Failed to connect to MySQL: " $mysqli->connect_error;
}

$res mysqli_query($mysqli"SELECT 'Possible but bad style.' AS _msg FROM DUAL");
if (!
$res) {
    echo 
"Failed to run query: (" $mysqli->errno ") " $mysqli->error;
}

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

Exemplul de mai sus va afișa:

Possible but bad style.

See also

add a note add a note

User Contributed Notes 1 note

up
0
Anonymous
9 months ago
After significant searching with no success finding a solution I was able to figure out the following to error 2005 where the connection could not find the host. While I am hosted on 1&1 I saw numerous posts with similar problems.

The solution turned out to be as follows:

<?php

$mysqli
= new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT, DB_UNIX_SOCKET);

I added the DB_PORT and DB_UNIX_SOCKET

/** name of the database. please note: database and database table are not the same thing! */
define("DB_NAME", "YOUR DB NAME");

/** user for your database. the user needs to have rights for SELECT, UPDATE, DELETE and INSERT.
/** By the way, it's bad style to use "root", but for development it will work */
define("DB_USER", "YOUR DB USER");

/** The password of the above user */
define("DB_PASS", "YOUR DB PWORD");

/** The unix socket for 1&1 dedicated */
define("DB_UNIX_SOCKET", "/tmp/mysql5.sock");

/** The db port, replace with yours */
define("DB_PORT", 3306);
?>
To Top