Thursday 29 Oct 2015

Convert class MDB2 in standard PHP MySQLi

PHPAs I did, you may have written some sites a few years ago with the class MDB2, equivalent to PDO, used to request a database with some additional useful functions, but mainly portability: these two classes use the same functions for a MySQL or PostgreSQL or other database...

However, if PDO was included in PHP and is always maintained, MDB2 has not really evolved over the last years. As this overlay losts some performance, it's probably wiser to do without now, especially if the hypothetical switch to PostgreSQL never happens! ;-)

~

To avoid rewriting all your sites or for the benefit of a few handy functions in MDB2, including the extension Extended (getRow, getCol, getOne...) I suggest the MDBDid class. Because it's very simple, there are some points to adjust for a conversion but it can be done by an automatic search/replacement.

If necessary, you must: replace « extended-> » by nothing, « $db =& MDB2::singleton(); » by « global $db; », « $r->getMessage() » by $db->error, and adapt the « PEAR::isError() » by using $db->error. This list is not exhaustive and you may have used other functions of the classes PEAR or MDB2 that are not rewrote.
But with these modifications, MDB2 should still work too, except for getAll which is not remade.

In the configuration files, the connection will be different:

require 'MDBDid.php';
global $db;
$db = @new MDBDid ('serveur', 'utilisateur', 'MdP', 'base');
if ($db->connect_error)
exit ('Error, no connection to the database.');
$db->query ("SET NAMES 'utf8'");

~

Here is the class MDBDid:


<?php
/* Please keep a link to http://azure-dev.kizone.net/648-mdb2
+ For MDB2 full compatibility, or we can remove this class and replace the functions query, exec and lastInsertId in the websites: +*/
class MDBDid_Result extends MySQLi_Result
{
public function fetchRow()
{
return $this->fetch_assoc();
}
 
public function numRows()
{
return $this->num_rows;
}
 
public function seek($déplacement)
{
return $this->data_seek($déplacement);
}
}
 
class MDBDid extends mysqli
{
public function query ($requête)
{
$this->real_query($requête);
return new MDBDid_Result($this);
}
public function exec ($requête)
{
return parent::query($requête);
}
public function lastInsertId()
{
return $this->insert_id;
}
/*--*/
 
public function getOne ($requête)
{
$résultat = parent::query ($requête);
if ($résultat === false)
return $this->error;
$rangée = $résultat->fetch_row();
$one = $rangée[0];
$résultat->close();
return $one;
}
 
public function getRow ($requête)
{
$résultat = parent::query ($requête);
if ($résultat === false)
return $this->error;
$rangée = $résultat->fetch_assoc();
$résultat->close();
return $rangée;
}
 
public function getCol ($requête)
{
$résultat = parent::query ($requête);
if ($résultat === false)
return $this->error;
$colonne = array();
while (is_array( $rangée = $résultat->fetch_row() ))
$colonne[] = $rangée[0];
$résultat->close();
return $colonne;
}
 
public function getAssoc ($requête)
{
$résultat = parent::query ($requête);
if ($résultat === false)
return $this->error;
$colonne = array();
while (is_array( $rangée = $résultat->fetch_row() ))
$colonne[$rangée[0]] = $rangée[1];
$résultat->close();
return $colonne;
}
}
?>


The comments are closed.

Azure Dev