Witam!
Tworze sobie taką klase, która ułatwi mi działanie na bazie danych. Na początku tworzyła automatycznie zapytania itp. Osobno łączyłem się z bazą (via mysqli), osobno tworzyłem zapytanie (moją klasą). Jednak chciałem to połączyć, więc rozrzeszyłem klase mysqli o moją klase.
<?php
class DBManager extends mysqli
{
private $selectables = array(); private $values = array(); private $table;
private $whereClause;
private $limit;
public $connection;
function __construct($host, $user, $pass, $base){
$this->connection = new mysqli($host, $user, $pass, $base);
return $this;
}
public function from($table)
{
$this->table = $table;
return $this;
}
public function where($clause)
{
$this->whereClause = $clause;
return $this;
}
public function limit($limit)
{
$this->limit = $limit;
return $this;
}
public function fields()
{
return $this;
}
public function select()
{
$query = "SELECT " . join(",", $this->selectables) . " FROM {$this->table}";
if (!empty($this->whereClause)){ $query .= " WHERE {$this->whereClause}";
}
if (!empty($this->limit)) $query .= " LIMIT {$this->limit}";
return $query;
}
public function update()
{
$query = "UPDATE {$this->table} SET " . join(",", $this->values) . " WHERE {$this->whereClause}"; return $query;
}
public function delete()
{
$query = "DELETE FROM {$this->table} WHERE {$this->whereClause}";
return $query;
}
public function add()
{
$query = "INSERT INTO {$this->table} (" . join(",", $this->fields) . ") VALUES ('" . join("', '", $this->values) . "')"; return $query;
}
}
$db = new DBManager("localhost", "root", "", "test");
$query = $db->from("posts")->where("id=4")->limit(1)->select("id", "title");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error
()); }
if ($result = $db->connection->query($query))
{
while ($obj = $result->fetch_object())
{
";
}
$result->close();
}
$db->connection->close();
?>
I niby wszystko fajnie działa, ale w metodzie "select" ten fragment kodu nie jest wykonywany:
if (!empty($this->whereClause)){ $query .= " WHERE {$this->whereClause}";
}
if (!empty($this->limit)) $query .= " LIMIT {$this->limit}";
Tak jakby $this->limit była pusta. A nie jest! Nadmienię, że wcześniej działało

I do tego, jeżeli zrobie coś takiego:
$a = $this->whereClause;
$query .= " WHERE {$this->whereClause}";
}
$a = $this->limit;
$query .= " LIMIT {$this->limit}";
To już wszystko śmiga. Totalnie nie mam pojęcia ocb.
Jeżeli ktoś ma jakieś pomysły, to śmiało walić

Pozdrawiam!
Każda dobrze napisana rzecz wciąga pod wode i zapiera dech...