Ik wil graag een (hele) oude class van MySQL naar MySQLi omzetten maar alle tutorials gaan over php (zonder class) waardoor moeilijker maakt en ik wil het aan passen zonder te slopen.
Dit is een makkelijke voorbeeld:
<?php
$conn = mysql_connect("db-host","user","pass");
$db = mysql_select_db("db-name", $conn);
$result = mysql_query("SELECT * FROM `table`");
?>
<?php
$conn = mysqli_connect("db-host","user","pass","db-name")
$result = mysqli_query($conn, "SELECT * FROM `table`");
?>
Dit is de class (verbinding) wat ik wil aanpassen en mij vraag is hoe pas ik dit aan zonder te slopen?
<?php
class mysqlclass {
private $dbHost = '';
private $dbName = '';
private $dbUser = '';
private $dbPass = '';
private $htmlWriter;
//Helpers
private $dbConnection;
private $dbResult;
//Debug informatie;
private $dbError;
private $dbErrno;
//Constructor in deze klasse word de verbinding met de mysql db gemaakt.
function __construct($ht){
$this->htmlWriter = $ht;
$this->dbConnection = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
if (!$this->dbConnection){
$this->dbError = mysql_error();
$this->dbErrno = mysql_errno();
throw new Exception($this->getErrors());
}
if (!mysql_select_db($this->dbName)){
$this->dbError = mysql_error();
$this->dbErrno = mysql_errno();
throw new Exception($this->getErrors());
}
}
//functie om een query uit te voeren.
function executeQuery($sql)
{
$this->htmlWriter->logDebugInfo("Executing: ".$sql."<br />\n");
$this->dbResult = mysql_query($sql);
if (!$this->dbResult){
$this->dbError = mysql_error();
$this->dbErrno = mysql_errno();
$msg = "<span style=\"color: red;\">Error while executing.<br />\n</span>\n";
$this->htmlWriter->logDebugInfo($msg);
throw new Exception($this->getErrors());
}
else{
$this->htmlWriter->logDebugInfo("Query executed.<br />\n");
}
}
//Functie om een resultaat in een array op te halen.
function getDataRow()
{
try{
$rows = mysql_fetch_array($this->dbResult);
if (count($rows) > 0){
mysql_free_result($this->dbResult);
}
return $rows;
}
catch (Exception $e){
throw new Exception($e->getMessage());
}
}
//Functie om het aantal opgehaalde rijen op te halen.
function getResultCount()
{
$counter = mysql_num_rows($this->dbResult);
$this->htmlWriter->logDebugInfo("Result count: ".$counter."<br />\n");
return $counter;
}
//Functie om het aantal aangepaste rijen op te halen.
function getAffectedRows()
{
$counter = mysql_affected_rows();
return $counter;
}
//Functie om een rij op te halen uit de database.
function getSingleRowAsObject()
{
$row = mysql_fetch_object($this->dbResult);
$this->htmlWriter->logDebugInfo("Geladen: ".$row->paginaID."<br />\n");
return $row;
}
//Functie om een aantal rijen op te halen uit de database.
function getRowsAsObjectArray()
{
$rows = array();
while ($row = mysql_fetch_object($this->dbResult)){
$rows[] = $row;
}
return $rows;
}
//Functie om een fout optehalen.
function getErrors(){
$retHtml = "<div class=\"errorDialog\">{$this->dbError}<br />\n{$this->dbErrno}</div>\n";
return $retHtml;
}
//Functie om de databse verbinding te sluiten.
function __destruct()
{
try{
$this->dbResult = null;
mysql_close($this->dbConnection);
}
catch(Exception $e){
throw new Exception($e->getMessage());
}
}
}
?>
Nu is de vraag wat heb ik allemaal gedaan?
- Eerst gezocht naar vergelijkbare class.
- Daarna PHP error geplaatst en dat geeft niet niks dan blanco scherm.
- PHP versie telkens verlaagd maar nog steeds blijft blanco scherm aangeven.
2.577 views