MySQLi connect

suggest change

Object oriented style

Connect to Server

$conn = new mysqli("localhost","my_user","my_password");

Set the default database: $conn->select_db("my_db");

Connect to Database

$conn = new mysqli("localhost","my_user","my_password","my_db");

Procedural style

Connect to Server

$conn = mysqli_connect("localhost","my_user","my_password");

Set the default database: mysqli_select_db($conn, "my_db");

Connect to Database

$conn = mysqli_connect("localhost","my_user","my_password","my_db");

Verify the connection

Since PHP 8.1 the default mysqli report mode throws mysqli_sql_exception when connect fails. The checks below run only if that mode is off (MYSQLI_REPORT_OFF).

Object oriented style

if ($conn->connect_errno > 0) {
    trigger_error($conn->connect_error);
} // else: successfully connected

Procedural style

if (!$conn) {
   trigger_error(mysqli_connect_error());
} // else: successfully connected

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents