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";



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s