Reply
[ARTICLE] PHP Easy MySQL Database Handler Class
Old 02-29-2008, 12:10 AM [ARTICLE] PHP Easy MySQL Database Handler Class
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,552
Name: Keith Marshall
Location: West Hartford, CT
Trades: 0
Easy MySQL Database Handler Class

Introduction:
Web development requires a lot of repeated functionality in every page you build. The way you handle these functions defines your web application. A well built application is a key to giving you, the developer, the proper tool to build powerful components and pages without the messy hassle and headaches of going through repeated code structures and lets you focus on the primary function you are working with.

Here is a suggestive method that you can use on your development structures with can handle MySQL queries with ease.

The Class Script:
You can download the attached file mySqlClass.inc.php (make sure the filename ends with the php extention).

To connect to the database, we need to define the connection login credentials with the proper username, password, database name and host. Create a file (something like configure.php) and write the following defined example replacing the generic value data with the correct values that fit your database connection. Save this file in a location where you would save other global includes for all of your application pages.


PHP Code:
<?php
  
//# Define database connection
  
define('DB_SERVER''localhost');
  
define('DB_SERVER_USERNAME''user_name_here');
  
define('DB_SERVER_PASSWORD''password_here');
  
define('DB_DATABASE''table_name_here');


Now to make the connection, we first include the configure.php file that defines our connection credentials. Then we include the actual class script file and start a new instance and call the connection method.

PHP Code:
<?php
//###  Start MySQL connection
  
require_once('configure.php');
  require_once(
'mySqlClass.inc.php');
  
$db = new mySqlClass();
  
$db->Connect(DB_SERVERDB_SERVER_USERNAMEDB_SERVER_PASSWORDDB_DATABASE);


Usage:
We can use the query method as if it were a standard mysql_query() function to pass a new SQL query. We assign this to a variable to capture the results.

PHP Code:
$result $db->SqlQuery("
  SELECT *
  FROM members
"
); 


All of the current results assigned to the new capture variable are stored as an associative array with the database table column names (or those specified within the query) are the keys. We can either print the value of the entire row array or a specific field column.

PHP Code:
// Print entire row:
print_r($result->field);
// Print just a specified field column
echo $result->field['id']; 


In order to iterate through all of the returned rows, we can loop it through a construct until all of the rows have been returned.

PHP Code:
while ($result->NextRow())
{
  
print_r($result->field);



Overview:
I hope this article helps you to achieve more manageable methods of coding and working with a database. This is intended to provide a learning tool for those who are interested in learning more about PHP and application development. The provided code is only for suggestion, and those who would like to offer suggestions and improvements are encouraged to do so.
Attached Files
File Type: txt mySqlClass.inc.php.txt (2.9 KB, 61 views)
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 02-29-2008, 02:54 AM Re: [ARTICLE] PHP Easy MySQL Database Handler Class
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 1,470
Name: Jeremy Miller
Location: Marianna, FL
Trades: 0
Thanks. Personally, I'd like to see more usage of MySQLi -- it's kind of like programming for PHP 4 now.

A class which releases MySQLi results when there's nothing useful there (e.g. when I do a multi_query and all queries are updates, I still have to clear out all the results) would be nice.

For the informed out there, I'm just not interested in the abstraction class that PHP 5 offers -- I've never had a need to code for anything other than MySQL and wouldn't have a clue if my SQL commands were compatible with an of the supported entities.
__________________
Jeremy Miller - TeraTask
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is online now
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 03-02-2008, 12:22 AM Re: [ARTICLE] PHP Easy MySQL Database Handler Class
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
Great information. Can this be a sticky thread?
joder is offline
Reply With Quote
View Public Profile
 
Old 04-07-2009, 10:15 AM Re: [ARTICLE] PHP Easy MySQL Database Handler Class
Junior Talker

Posts: 1
Name: Vali Oancea
Trades: 0
Hi.

I just tested and there is an error in class on line 33:

Quote:
if (!$result_resource) $this->set_error(@mysql_errno($this->link), @mysql_error($this->link));
it must be

Quote:
if (!$result_resource) $this->_error(@mysql_errno($this->link), @mysql_error($this->link));
Nice job.
voancea is offline
Reply With Quote
View Public Profile
 
Old 04-07-2009, 10:57 AM Re: [ARTICLE] PHP Easy MySQL Database Handler Class
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,552
Name: Keith Marshall
Location: West Hartford, CT
Trades: 0
Quote:
Originally Posted by voancea View Post
Hi.

I just tested and there is an error in class on line 33:



it must be



Nice job.
Thanks! I see it on Line 45 though, but a mistake none-the-less!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-07-2009, 11:09 AM Re: [ARTICLE] PHP Easy MySQL Database Handler Class
stevej's Avatar
Professional Multitasker

Posts: 991
Location: In a flying house
Trades: 0
Thanks, mgraphic. You did a marvelous job on this post. I never liked databases in the past because I thought they were so confusing, but this post will certainly change my views.

- Steve
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to [ARTICLE] PHP Easy MySQL Database Handler Class
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.15081 seconds with 14 queries