Up to now, I was considering MySQLi functions in PHP one example of DAL. And I initiated a simple project to publish in an article with the title "starting with PHP and DAL and MySQL"
But when I made a search for some topics to add in my article I "discovered" two things:
- The website PHP.net don't list MySQLi in there page about DAL
- Many articles speech about MySQLi as library or API, but I didn't found anyone stating it as DAL.
And now I have this question: Why the class PDO is considered to be a DAL example, but the class MySQLi isn't considere a DAL example ?
What I was thinking to write is something like that:
If you create a class named Container, like the code bellow, it's easy to see the access to DAL by functions: mysqli_connect, mysqli_query, and the others mysqli_functions
// this is a very basic implementation, and need many improvements, it is just an example
class Container
{
private $_Connection = null;
private $_Fields = null;
function __construct($host, $user, $password, $database)
{
$connection = mysqli_connect($host,$user,$password);
if( $connection )
{
mysqli_select_db($connection, $database);
mysqli_set_charset($connection, "utf8");
$this->_Connection = $connection;
}
}
function __destruct()
{
if( $this->_Connection )
{
mysqli_close($this->_Connection);
}
}
function Query( $sql )
{
$dataset = array();
$result = mysqli_query( $this->_Connection, $sql );
if ($result)
{
$this->_Fields = mysqli_fetch_fields($result);
while($data = mysqli_fetch_assoc($result))
{
$dataset[] = $row;
}
}
return $dataset;
}
function Fields( $index )
{
if(isset($index))
{
return $this->_Fields[$index];
}
else
{
return $this->_Fields;
}
}
}
16/02/17:
What if I change my class Container constructor to something like:
function __construct($driver, $host, $user, $password, $database)
{
switch( $driver )
{
case "MYSQL": $connection = mysqli_connect($host,$user,$password);
if( $connection )
{
mysqli_select_db($connection, $database);
mysqli_set_charset($connection, "utf8");
$this->_Connection = $connection;
}
break;
case "SQLServer": // to do...
case "Oracle": // to do...
}
}
And make similar modifications in Container::Query and Container::Fields methods.
So, in this case, my class Container will be an example of DAL ?
17/02/17:
Can someone create a DAL for an specific database ?
Like this: https://github.com/mdandy/PHP-DAL
This is a Data Access Layer for MySQL