PHP mysqli_connect() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_connect() opens a new connection to a MySQL server and returns a mysqli object on success.

Object-oriented style uses new mysqli($host, $user, $password, $database, $port, $socket). On failure the procedural form returns false; always check connect_errno or enable exception reporting before querying.

Syntax

Call mysqli_connect() with the values you need. Start with this line.

PHP
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
CallWhat it does
mysqli_connect("localhost", "demo", "secret", "shop")Connects with host, user, password and database.
new mysqli("localhost", "demo", "secret", "shop")Object-oriented constructor form.
mysqli_connect("127.0.0.1", "demo", "secret", "shop", 3306)Connects with an explicit TCP port.

After connecting, call set_charset('utf8mb4'). For failures, mysqli_connect_errno() and mysqli_connect_error() explain what went wrong. Persistent links use a p: host prefix and are an advanced topic.

Library declarations
PHP
mysqli_connect(?string $hostname = null, ?string $username = null, ?string $password = null, ?string $database = null, ?int $port = null, ?string $socket = null): mysqli|false

Parameters

ParameterDescription
hostnameOptional. Server host, or null for the default.
usernameOptional. MySQL user name.
passwordOptional. Password for that user.
databaseOptional. Default database to select.
portOptional. TCP port number.
socketOptional. Unix socket path when not using TCP.

Return Value

Return ValueDescription
mysqli|falseA mysqli connection object on success, or false on failure (procedural style).

PHP Examples

Example 1 — Connect and Test With SELECT 1

Open a connection and run a tiny query. Sample output when connected to a demo database.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
if (!$mysqli) {
    echo "Connect failed", PHP_EOL;
    exit;
}

$result = mysqli_query($mysqli, "SELECT 1 AS ok");
$row = mysqli_fetch_assoc($result);
echo "ok=", $row["ok"], PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_connect() returns a mysqli object or false.
  2. The failure branch exits early if connect fails.
  3. SELECT 1 is a safe connectivity check.
  4. Sample output when connected to a demo database prints ok=1.

Example 2 — Object Style Constructor

Create the connection with new mysqli and set utf8mb4.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
if ($mysqli->connect_errno) {
    echo "Connect error: ", $mysqli->connect_error, PHP_EOL;
    exit;
}
$mysqli->set_charset("utf8mb4");
echo "Connected as utf8mb4", PHP_EOL;
$mysqli->close();

How It Works

  1. new mysqli() attempts the connection.
  2. connect_errno is checked before any query.
  3. set_charset() locks in utf8mb4.
  4. A success message is printed, then close().

Example 3 — Handle a Failed Connection

Show the connect error helpers when login fails.

PHP
<?php
$mysqli = @mysqli_connect("localhost", "demo", "bad-password", "shop");
if (!$mysqli) {
    echo "errno=", mysqli_connect_errno(), PHP_EOL;
    echo "error=", mysqli_connect_error(), PHP_EOL;
}

How It Works

  1. A wrong password is used on purpose for the demo.
  2. mysqli_connect() returns false.
  3. mysqli_connect_errno() gives the numeric code.
  4. mysqli_connect_error() gives the human-readable message.

Frequently Asked Questions

A mysqli object when the connection works, or false when it fails. Always check the result before running queries.
Use $mysqli = new mysqli($host, $user, $password, $database). Then check $mysqli->connect_errno.
mysqli_connect() opens in one step. mysqli_init() plus mysqli_real_connect() lets you set options before the actual handshake.
Use utf8mb4 with mysqli_set_charset() unless you have a rare legacy reason not to.
Outside the web root, in environment variables or a private config file that is not committed to public repositories.

Next: mysqli_connect_errno()

mysqli_connect_errno() returns the error code from the last connection attempt.

mysqli_connect_errno() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful