<?php
class Order
{
private $host = "localhost";
private $username = "user";
private $password = "pass";
private $db = "db";
private $mysqli;
public function __construct()
{
}
public function __destruct()
{
$this->mysqli->close();
}
public function saveOrder($uid, $timestamp)
{
# check connection
if (mysqli_connect_errno($this->mysqli))
$this->connect();
$stmt = $this->mysqli->prepare("INSERT INTO orders SET uid = ?, date = ?, status = 'new'");
$stmt->bind_param("ii", $uid, $timestamp);
$stmt->execute();
$oid = $stmt->insert_id;
$stmt->close();
return $oid;
}
private function connect()
{
$this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->db);
}
}
?>
I get an error when calling Order::saveOrder(): "Call to a member function prepare() on a non-object". The reason for this is that the mysqli connection is NULL in saveOrder(), but it connects fine in connect(). The connection check in saveOrder() doesn't help...
I use this class like this:
<?php
$order = new Order();
$oid = $order->saveOrder($iud, $timestamp);
?>
Any suggestion on solution to this?