<?php
// Open the database connection
if (!($db mysqli_connect('localhost''guest''secret''dbname'))) {
    
// Handle errors
    
die('SQL ERROR: Connection failed: ' mysqli_error($db));
}

// Prepare a SQL insert: assume we received the description from the user
$desc 'Mortising Machine';
$escaped mysqli_escape_string($desc);
$sql "insert into tools (descr, qty) values ('{$escaped}', 0)";

// Do the insertion:
if (!(mysqli_query($db$sql))) {
    
// Give an error statement:
    
die('SQL INSERT ERROR: ' mysqli_error($db). " - Query was: {$sql}");
}

// Select data from the table:
$sql 'select id, descr from tools order by id asc';
if (!(
$result mysqli_query($db$sql))) {
    
// Give an error statement:
    
die('SQL SELECT ERROR: ' mysqli_error($db). " - Query was: {$sql}");
}

// Read all the data back in as associative arrays
while ($row mysqli_fetch_assoc($result)) {
    echo 
"{$row['id']}. {$row['descr']}<br />\n";
}

// Now close the connection
mysqli_close($db);
?>