PHP MySQL: MySQLi procedural tutorial

The deprecated mysql extension can be easily upgraded to the future proof mysqli, that supports both a procedural and an object-oriented syntax (creating some confusion at first when you find either one or the other in public forums…).
The procedural syntax is quite similar to the previous one, except for the mandatory “connection” argument.

$conn = mysqli_connect($host, $user, $password, $database);
mysqli_select_db($conn, $database);
if (mysqli_connect_errno($conn)) {
trigger_error('DB conn failed: ' . mysqli_connect_error(), E_USER_ERROR);

For a typical query:

$sql = "SELECT * FROM ...";
$result = mysqli_query($conn, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ". mysqli_error($conn), E_USER_ERROR);

while ($row = mysqli_fetch_array($run)) {
print "$row[id]...\n";