Example 1 — Fetch One Row as an Associative Array
Run a SELECT, then read the first row with MYSQLI_ASSOC. The printed values are a fixed sample.
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, name FROM products ORDER BY id LIMIT 1");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
echo $row["id"], " ", $row["name"], PHP_EOL;
mysqli_free_result($result);
mysqli_close($mysqli); How It Works
- mysqli_connect() opens a demo connection to the shop database.
- mysqli_query() runs a SELECT that returns one product row.
- mysqli_fetch_array() with MYSQLI_ASSOC returns that row keyed by column name.
- We print id and name, then free the result and close the connection.

