PHP Chaining: how to get data from a mysql table?
I am trying to implement PHP chaining method in my web development
project. But I seem can't get it right.
class foo extends base{
public $query = null;
public $item = array();
public function __construct($connection){
parent::__construct($connection);
}
public function select($query){
$this->query = $query;
return $this;
}
public function where($query){
$this->query = $query;
return $this;
}
public function __toString()
{
$this->item = $this->connection->fetch_assoc($this->query);
return var_export($this->item, true);
}
}
$connection = new db($dsn = 'mysql:host=localhost;dbname=xxx',$username =
'xxx',$password = 'xxx');
$foo = new foo($connection);
$select = $foo->select("SELECT * FROM page")->where("page_id = 10 ");
print_r($select->item);
the result I get,
Array
(
)
But I should get a row of data. Just like I normally do it in this way,
class boo extends base{
...
public function select() {
$sql = "
SELECT *
FROM page
WHERE page_id = ?
";
$item = $connection->fetch_assoc($sql,array(1));
return $item;
}
}
What have I missed in my chaining method?
No comments:
Post a Comment