Database Abstraction
For our shopping cart we will use a simple database abstraction library. Although this tutorial only use MySQL but using an abstraction layer is still a good thing. We do this just in sow we can easily migrate to another database. In case you don't know, a database abstraction is simply making our own interfaces ( functions ) to call the database function provided by PHP. This way if we change to another database or if the PHP function itself change we just need to modify one file ( database.php ) instead of modifying all our source code. The database functions is stored in library/database.php. You can see the content below |
|
<?php
require_once 'config.php'; $dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect
failed. ' . mysql_error()); function dbQuery($sql) function dbAffectedRows() function dbFetchArray($result, $resultType = MYSQL_NUM) function dbFetchAssoc($result) function dbFetchRow($result) function dbFreeResult($result) function dbNumRows($result) function dbSelect($dbName)
How to use it ?Below you can see an example on how to use this database abstration library. The first one is using PHP database function directly : <?php
require_once 'config.php'; $conn = mysql_connect($dbHost, $dbUser, $dbPass); $sql = "SELECT * $result = mysql_query($sql); $products = array(); And the second one is using our database abstraction. We don't have to initiate the connection this time because it is done from database.php : <?php
require_once 'config.php'; require_once 'database.php'; $sql = "SELECT * $result = dbQuery($sql); $products = array(); As you can see, using database abstraction isn't so hard at all. Now that we have taken care the database issues we start building the admin pages. |
|
|
|
|
Database Design | PHP MySQL Shopping Cart Tutorial : Database Abstraction | Admin Control Panel |
|
At long last i'm finally able to update this site. Now the shopping cart can handle payment through paypal. As always you have any critiques, questions, comments or problems about this tutorial please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a lot :-) |
PHP MySQL Shopping Cart Tutorial
Copyright © 2005 - 2008 www.phpwebcommerce.com