错误意外T_VARIABLE PDO

时间:2013-05-17 18:14:41

标签: php pdo

我似乎无法找到为什么我会收到意外的T_variable错误。我试图用一个类来调用我的数据库。 错误发生在第18行

 $query = $this->link->query("SELECT * FROM markers WHERE 1");  

这是我的全班。

任何线索?

 class Users{
        private $link; 
        public function __construct(){
        $this->link = new Connection();
        $this->link = $this->link->dbConnect();
    }

   $query = $this->link->query("SELECT * FROM markers WHERE 1");  
   $query->$this->link->setFetchMode(PDO::FETCH_ASSOC);

    header("Content-type: text/xml");

      while($row = $stmt->fetch()) {  

            $node = $dom->createElement("marker");
            $newnode = $parnode->appendChild($node);
            $newnode->setAttribute("name", $row['name']);
            $newnode->setAttribute("adress", $row['adress']);
            $newnode->setAttribute("lat", $row['lat']);
            $newnode->setAttribute("lng", $row['lng']);
            $newnode->setAttribute("type", $row['type']);
      }
       echo $dom->saveXML();
}
第18行和第19行是

$query = $this->link->query("SELECT * FROM markers WHERE 1");  
$query->$this->link->setFetchMode(PDO::FETCH_ASSOC);

1 个答案:

答案 0 :(得分:3)

你要么:

  • 忘记在构造函数
  • 之后关闭User类
  • 忘记将代码包含在类方法中

例如:

class Users{
    private $link; 
    public function __construct(){
      $this->link = new Connection();
      $this->link = $this->link->dbConnect();
    }
}

或者:

class Users{
    private $link; 
    public function __construct(){
    $this->link = new Connection();
    $this->link = $this->link->dbConnect();
}

  public function someMethod() {
    $query = $this->link->query("SELECT * FROM markers WHERE 1");  
    $query->$this->link->setFetchMode(PDO::FETCH_ASSOC);

    header("Content-type: text/xml");

    while($row = $stmt->fetch()) {  

        $node = $dom->createElement("marker");
        $newnode = $parnode->appendChild($node);
        $newnode->setAttribute("name", $row['name']);
        $newnode->setAttribute("adress", $row['adress']);
        $newnode->setAttribute("lat", $row['lat']);
        $newnode->setAttribute("lng", $row['lng']);
        $newnode->setAttribute("type", $row['type']);
    }
    echo $dom->saveXML();
  }
}