I have tried for to make a php database connection using a class which finally works. My Class looks like this:
class Db {
protected static $connection;
public function connect() {
if(!isset(self::$connection)) {
self::$connection = new mysqli('localhost','root','','Personenverzeichnis');
}
if(self::$connection === false) {
return false;
}
return self::$connection;
}
public function query($query) {
$connection = $this -> connect();
$result = $connection -> query($query);
return $result;
}
public function select($query) {
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
public function error() {
$connection = $this -> connect();
return $connection -> error;
}
public function quote($value) {
$connection = $this -> connect();
return "'" . $connection -> real_escape_string($value) . "'";
}
}
Then I use this class in my php-Code like this:
$db = new Db();
$abfrage = "SELECT `Date`,`Keyword` FROM `notes` WHERE `PERSON_ID` =
2 ORDER BY `Date` DESC; ";
$resultat = $db -> query($abfrage);
while($daten = $resultat->fetch_object() ){
echo "<p>date ".$daten->Date.": ".$daten->Keyword."</p>";
}
This way it works perfectly fine. But how can I use prepare() method?
I tried this code which does not work. Can anybody help me?
$db = new Db();
$abfrage = "SELECT `Date`,`Keyword` FROM `notes` WHERE `PERSON_ID` = 2
ORDER BY `Date` DESC; ";
$resultat = $db->prepare($abfrage);
$resultat->execute();
$resultat->bind_result($date, $keyword);
while($daten = $resultat->fetch_object() ){
echo $date .": " .$keywords ."<br>";
}
The methode prepare is unknown. How to solve this problem?