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’;
$conn = mysql_connect($dbHost, $dbUser, $dbPass);
mysql_select_db($dbName);
$sql = “SELECT *
FROM tbl_product
ORDER BY pd_name”;
$result = mysql_query($sql);
$products = array();
while ($row = mysql_fetch_assoc($result)) {
$products[] = $row;
}
?>
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 :
$products = array();
while ($row = dbFetchAssoc(&$result)) {
$products[] = $row;
}
?>
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.
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’;
require_once ‘config.php’;
$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die (‘MySQL connect failed. ‘ . mysql_error());
mysql_select_db($dbName) or die(‘Cannot select database. ‘ . mysql_error());
mysql_select_db($dbName) or die(‘Cannot select database. ‘ . mysql_error());
function dbQuery($sql)
{
return mysql_query($sql) or die(‘Query failed. ‘ . mysql_error());
}
{
return mysql_query($sql) or die(‘Query failed. ‘ . mysql_error());
}
function dbAffectedRows()
{
global $dbConn;
return mysql_affected_rows($dbConn);{
global $dbConn;
}
function dbFetchArray($result, $resultType = MYSQL_NUM)
{
return mysql_fetch_array($result, $resultType);
}
{
return mysql_fetch_array($result, $resultType);
}
function dbFetchAssoc($result)
{
return mysql_fetch_assoc($result);
}
{
return mysql_fetch_assoc($result);
}
function dbFetchRow($result)
{
return mysql_fetch_row($result);
}
{
return mysql_fetch_row($result);
}
function dbFreeResult($result)
{
return mysql_free_result($result);
}
{
return mysql_free_result($result);
}
function dbNumRows($result)
{
return mysql_num_rows($result);
}
{
return mysql_num_rows($result);
}
function dbSelect($dbName)
{
return mysql_select_db($dbName);
}
?>
{
return mysql_select_db($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);
mysql_select_db($dbName);
$sql = “SELECT *
FROM tbl_product
ORDER BY pd_name”;
$result = mysql_query($sql);
$products = array();
while ($row = mysql_fetch_assoc($result)) {
$products[] = $row;
}
?>
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 *
FROM tbl_product
ORDER BY pd_name”;
$result = dbQuery($sql);require_once ‘config.php’;
require_once ‘database.php’;$sql = “SELECT *
FROM tbl_product
ORDER BY pd_name”;
$products = array();
while ($row = dbFetchAssoc(&$result)) {
$products[] = $row;
}
?>
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.
0 komentar:
Posting Komentar