Example 1 — Init Then Real Connect
Create a handle, connect, then print host info. Sample output is fixed.
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
echo "Init failed.", PHP_EOL;
exit;
}
$ok = mysqli_real_connect(
$mysqli,
"localhost",
"demo_user",
"demo_pass",
"app_db"
);
echo $ok ? "Connected." : "Connect failed.", PHP_EOL;
mysqli_close($mysqli); How It Works
- mysqli_init() builds an empty mysqli object.
- mysqli_real_connect() opens the TCP/socket session.
- On success the handle works like a normal connection.
- Close when finished.

