Hoi,
Ik heb onderstaande pdo Class en contacts class
nu wil ik in de functie getcontacts() de functie query() uit de database class aanroepen die het resultaat uit deze query teruggeeft.
Ik heb van alles geprobeerd maar denk dat ik iets over het hoofd zie.
iemand tips over wat ik op de vraagtekens moet plaatsen om dit voor elkaar te krijgen?
class.database.php
<?php
include ('config.php');
class database extends PDO {
private $log;
public $debug = FALSE;
//
// Constructor - Create database connection
//
function __construct($dblogin)
{
// Exception afvangen, anders komen de logingegevens in tekst als foutmelding!
try
{
PDO::__construct('mysql:host=localhost;dbname='.$dblogin['db'], $dblogin['user'], $dblogin['pass']);
//parent::__construct('mysql:host=127.0.0.1;dbname='.$dblogin['db'], $dblogin['user'], $dblogin['pass']);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
if ($this->debug) echo $e->getMessage();
}
}
//
// Execute query.
//
function query($sql)
{
$starttime = microtime(TRUE);
try
{
$stmt = parent::query($sql);
$stmt->execute();
$results = $stmt->fetchAll();
}
catch (PDOException $e)
{
if ($this->debug) echo $e->getMessage();
}
$this->log[] = array('sql'=>$sql,'time'=>microtime(TRUE)-$starttime);
return (isset($results)) ? $results : NULL;
}
//
// Show table with executed queries and execution times
//
function showLog()
{
$html = '<table border="1">';
foreach ($this->log as $item)
$html.= '
<tr>
<td>'.sprintf('%10.2f',1000*$item['time']).' ms</td>
<td><pre>'.$item['sql'].'</pre></td>
</tr>';
$html .= '</table>';
return $html;
}
}
?>
contacts_model.php
<?php
class contacts {
public function getcontacts()
{
$sql = "SELECT * from contacts";
????????????function query($sql)??????????????
return ?????resultaat van de query??????
}
}
?>
2.093 views