내 웹 개발 프로젝트에서 PHP 체인 방법을 구현하려고합니다. 하지만 나는 제대로 이해할 수없는 것 같습니다.PHP 체인 : mysql 테이블에서 데이터를 가져 오는 방법은 무엇입니까?
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);
내가 할 결과,
Array
(
)
하지만 데이터의 행을 얻어야한다. 정상적으로 이렇게하는 것처럼,
체인 방법에 무엇이 없습니까?
편집 :
class base
{
protected $connection = null;
public function __construct($connection)
{
$this->connection = $connection;
}
}
난 그냥
print_r($select);
결과,
foo Object
(
[query] => where page_id = 10
[item] => Array
(
)
[connection:protected] => db Object
(
[connection] => PDO Object
(
)
[dsn] => mysql:host=localhost;dbname=xxx
[username] => xxx
[password] => xxx
)
)
감사합니다. 쿼리를'체인화 (chain) '하고 실행시킬 수 있습니까? – laukok
'기본 '은 어떻게 구현 되나요? 그 질문을 넣어도 될까요? print_r ($ select-> item) 대신'print_r ($ select)'처럼 선택하여 인쇄 해보십시오. – vee
'base'는 db 연결의 속성을 유지하기위한 것입니다. 위의 편집을 참조하십시오. 감사합니다 – laukok