Web Services from Awave, Inc.
Communications systems and services from Awave, Inc.
Computer & Network Systems and Services from Awave, Inc.
Video and Multimedia Systems and Services from Awave, Inc.
Consulting Services from Awave, Inc.
Contact Awave, Inc.
 

BF_logo_04_IS

PHP.net
MySQL Site
WebSite Pros

Ulead PhotoImpact  

Highly Recommended!

Visit these sites where we use what we teach here:

The Edge

A commercial site where really cool tie-dye clothing is sold. Uses PHP generated links to Mal's Shopping Cart.
 

Scifisource

Directory of Science Fiction and Fantasy sites. Search and category results pages use PHP generated tables to display results.

Online PHP Resources

Zend
Their engine powers PHP. They own PHP.

PHPBuilder
Good general PHP tutorials and code library

HotScripts
Large library of PHP and other code snippets and programs

Tutorial:
Using PHP and MySQL with Fusion

© 2000-2008
Awave, Inc.

Tutorial - Get Fusion 10
dynamic web sites using PHP and MySQL

Contact the author

Lesson 4 - Using PHP to Connect to a MySQL Database

Basic Method -

Below is an example of connecting to a MySQL server using PHP.
I've placed the code into the box for you.

    Note: the box is the background image of a layout region. A very useful technique.

Example 1

<?
 @ $db = mysql_pconnect("localhost", "username", "password");

 if (!$db)

 {
   echo "<p>Error: Could not connect to database. Please try again later.</p>";
 }
?>

This snippet defines a variable ($db) containing the connection string for the PHP mysql_pconnect function and executes the connection function.

If the database cannot be reached, the 'if' statement returns an error message. Otherwise, a persistent connection is established. Using 'mysql_pconnect' rather than the alternative 'mysql_connect' can improve performance on a busy server.

Actually connecting to the appropriate database on the server comes next.

Ok....so where do I place the code in Fusion?

Excellent question! The answer is: It depends.

It depends on what you are trying to accomplish really (more about that later), but in almost all cases simply placing it in a text box inside the layout region where you want to display output to the user is appropriate.

A Better Method -

Using the method above works fine, but if you have many pages that use PHP and change your password periodically (as you should for security reasons) it can be a chore to have to change the password manually on each and every page in your site.

Enter... user defined functions in PHP. In the following examples, you see a function that can be included (as shown) on each of your PHP-enabled pages. Using this or a similar function means you only have to change your password in one place. A good thing..

 <?
 
  // connect to your database
  function db_connect()
  {
    $result = @mysql_pconnect("localhost", "username", "password");
    if (!$result)
     return false;
    if (!@mysql_select_db("yourdatabase"))
     return false;
 
    return $result;
  }
 ?>

Important! Save this code in a text file in Notepad and name it "dbconn.php" This file will then need to be FTP'd to the directory on the web server where your pages are stored.

Example: /www/website/htdocs/html (if you publish by Asset Type in Fusion)

Note: if you use PHP on your index page, you'll need to modify the following Include example to show the relative path to the file. Otherwise, PHP will look in the default 'include directory' on the server - which won't work. Or you can place a copy of dbconn.php in the document root directory also - but that almost defeats the purpose, doesn't it.

Example 2

<?
 include('dbconn.php');

 if (!db_connect())

 {
   echo "<p>Error: Could not connect to database. Please try again later.</p>";
 }

Substitute the code above for the code in Example 1 in your script.

The code below should immediately follow in your script and resumes the actual connection to your database on the server: 

 else
 {
  mysql_select_db("yourdatabase");

PHP code goes here that performs the desired task

 }
?>

What's next? -

Now that you know how to establish a connection to your database, we'll explore some useful examples for accessing data from a "live" database and display it on a web page. That will get you some "instant gratification". Afterwards, we'll show you how to add and update records.

Lesson 5 - Displaying Records with PHP

Last Updated: 01/27/08

allwebmenus - Cross platform DHTML!