Example 1 — Close After a Simple Query
Open, query, then close. Sample output when connected to a demo database.
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT 1 AS one");
$row = mysqli_fetch_assoc($result);
echo $row["one"], PHP_EOL;
mysqli_close($mysqli); How It Works
- Connect with demo credentials.
- Run a tiny SELECT to prove the link works.
- Print the value from the result row.
- mysqli_close() ends the session cleanly.

