PHP & MySQL > PHP MySQL Connection class
Date: Friday, July 16, 2010 - 22:25
Difficulty: Begginer
Views: 3626
Pages: 3

Author: Saeid Yazdani

Developing a website nowadays is useless unless you deal with storing and retrieving data from a database. PHP and MySQL are most famous server side scripting language combination currently available. Although it is quite possible to deal with MySQL through built-in PHP functions, an inter mediating class can make the job a lot easier. in this article I provide you with a MySQL connection class that will help you doing MySQL queries and data retrieval in an easier way.
Page 1:  Setting up MySQL connection class

In this article I will explain how to setup and get the promised MySQL connection class to work. First of all download the php file from here: MySQL Connection class, Password for opening the zip file is: electronicspub.com

Requirements:

  • PHP v4.0 or higher
  • MySQL v3 or higher
  • Webserver or local server supporting PHP and MySQL

If you are going to follow this article by using a Webserver supporting PHP and MySQL, you should be fine. IF not, you can download a nice local server program that contains most of your PHP developing base named XAMPP.

After unzipping the file, you will find a file named mysql.php, Upload it into your server or local driver where your local server can parse php files. usually htdocs if you are using XAMPP.

As we are going to work on a test table, make sure you will have the following table in your MySQL Database, Either you can make this table manually or use a third party software like phpMyAdmin. So first create a database named test (or use an existing database but make sure to change test to your own database name in the provided examples) and then make a table like this:

CREATE TABLE electronicspub (
 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `author` VARCHAR( 60 ) NOT NULL ,
 `news` TEXT NULL ,
 `date` DATETIME NOT NULL
) ENGINE = MYISAM ;

After succesfully creating the table, open your editor an make a file named setup.php and include the class (note that it can be in any folder in your webserver or local server, it shouldn't be a problem unless you give the include function a wrong path to the mysql.php file.

<?php
    //SETUP.PHP

    //YOUR MySQL CONNECTION DETAILS
    $DATABASE["server"] = "MYSQL SERVER ADDRESS, default = localhost";
    $DATABASE["port"] = "PORT NUMBER, DEFAULT 3306";
    $DATABASE["username"] = "DATABASE USERNAME";
    $DATABASE["password"] = "DATABASE PASSWORD";
    $DATABASE["database"] = "DATABASE NAME";

    
    
    //Including mysql.php , Remember to give the right path to this file
    include "mysqlphp";

    // Initialaizing connection to database using CLS_MYSQL  
    $_DB = new CLS_MYSQL($DATABASE);
    
    //Connecting to database
    $_DB->Connect();
?>

So you have intialaized the class by assigning a variable named $_DB.

Now, create another php file (name does not matter, but I make one named test.php), Then it is time to populate our table with some data using this connection class. go to next page!


<< Pervious page
Next page >>

EETUTORIALS.COM © 2010 | Design and developement: Saeid Yazdani