PEAR Articles

  Home arrow PEAR Articles arrow A PEAR CMS: Examining the Code
PEAR ARTICLES

A PEAR CMS: Examining the Code
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-10-08

    Table of Contents:
  • A PEAR CMS: Examining the Code
  • Connecting to the Database
  • Styles
  • Edit Sections

  •  
     

    SEARCH CODEWALKERS

    A PEAR CMS: Examining the Code


    (Page 1 of 4 )

    In the last article we looked at some of the scripts that are going to make up our CMS system. Codewise, this Content Management System is very simple, but will form a very good basis for a custom-made CMS. It is also a very good way to learn PEAR and database abstraction.

    A walk through the code

    In the last article, we looked at the “connx.php” configuration file, but we did not go through the code, so lets take a look at it:

    <?php


    // the hostname of the database server

    // the username to connect with

    // the user's password

    // the name of the database to connect to

    // the type of database server.

    $dbhost= "localhost";

    $dbuser= "root";

    $dbpass= "apass";

    $dbname= "cms";

    $dbtype= "mysql";


    // Build a DSN string (Data Source Name)

    // Required by DB::connect()

    $dsn = $dbtype . "://"

    .$dbuser . ":"

    . $dbpass . "@"

    . $dbhost . "/"

    . $dbname;


    // Creates a database connection object in $db

    // or, a database error object if it went wrong.

    // The boolean specifies this is a persistent

    // connection like mysql_pconnect(), it

    // defaults to FALSE.

    $db = DB::connect($dsn, TRUE);


    // Check whether the object is a connection or

    // an error.

    // Print out a message and exit if it's

    // an error object.

    if (DB::isError($db)) {

    die($db->getMessage());

    }


    ?>

    Basically, this script is of such importance that it will be included in every script that uses the database. It contains all the information that is required by the DB class to successfully connect to a database server. The code is heavily commented for ease of reading.

    It starts off by defining some connection details. This part of the script tells the server about the host, password and some other information that is required to connect to a database:

    // the hostname of the database server

    // the username to connect with

    // the user's password

    // the name of the database to connect to

    // the type of database server.

    $dbhost= "localhost";

    $dbuser= "root";

    $dbpass= "apass";

    $dbname= "cms";

    $dbtype= "mysql";

     The next part builds a Data Source Name or DSN. It is required by the DB::connect() function. As you will see in the next block of code, it will be used to initiate the connection:

    // Build a DSN string (Data Source Name)

    // Required by DB::connect()

    $dsn = $dbtype . "://"

    .$dbuser . ":"

    . $dbpass . "@"

    . $dbhost . "/"

    . $dbname;

    The DSN will then look something like this:

    $dsn = $dbtype://$dbuser:$dbpass:$dbhost:$dbname;

    More PEAR Articles Articles
    More By David Web

    blog comments powered by Disqus

    PEAR ARTICLES ARTICLES

    - Installing PEAR
    - PEAR: an Introduction
    - Managing robots.txt using PHP: Generating Dy...
    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap