PhpRiot
Follow phpriot on Twitter
Sponsored Link
Download Article
Download this article in PDF format with all listings and files.

Includes 1 bonus listing!

Price: $5.00 AUD
(Approx. $5.00 USD)

More information
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Storing Images in MySQL Revisited

Connecting to the Database

Let's now look at some PHP code to connect to the database. We're going to include this in a global PHP script that we'll include from other scripts.

In this code, we first try and connect to the database server. Once that connection is made we try and select our database. If either of these steps fails we output an error message and exit.

Listing 4 Connecting to the database (globals.php)
<?php
    $db = mysql_connect('localhost', 'phpriot_demo', 'phpriot123');
 
    if (!$db) {
        echo "Unable to establish connection to database server";
        exit;
    }
 
    if (!mysql_select_db('phpriot_demo', $db)) {
        echo "Unable to connect to database";
        exit;
    }
?>

We will include this script at the start of every other PHP script used in this article.

In This Article



Bonus listings: 1 available