Home PHP MySQLi mysqli_connect() PHP mysqli_connect() Function 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.
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop"); Call What 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 mysqli_connect(?string $hostname = null, ?string $username = null, ?string $password = null, ?string $database = null, ?int $port = null, ?string $socket = null): mysqli|false Parameters Parameter Description hostname Optional. Server host, or null for the default. username Optional. MySQL user name. password Optional. Password for that user. database Optional. Default database to select. port Optional. TCP port number. socket Optional. Unix socket path when not using TCP.
Return Value Return Value Description mysqli|false A mysqli connection object on success, or false on failure (procedural style).
PHP Examples 1. Connect and Test With SELECT 1 2. Object Style Constructor 3. Handle a Failed Connection 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
$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 mysqli_connect() returns a mysqli object or false. The failure branch exits early if connect fails. SELECT 1 is a safe connectivity check. 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
$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 new mysqli() attempts the connection. connect_errno is checked before any query. set_charset() locks in utf8mb4. A success message is printed, then close(). Frequently Asked Questions What does mysqli_connect() return? A mysqli object when the connection works, or false when it fails. Always check the result before running queries.
How do I connect with object-oriented style? Use $mysqli = new mysqli($host, $user, $password, $database). Then check $mysqli->connect_errno.
What is the difference between connect and real_connect? mysqli_connect() opens in one step. mysqli_init() plus mysqli_real_connect() lets you set options before the actual handshake.
Which charset should I set after connect? Use utf8mb4 with mysqli_set_charset() unless you have a rare legacy reason not to.
Where should I put database passwords? Outside the web root, in environment variables or a private config file that is not committed to public repositories.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion