【发布时间】:2016-01-07 17:51:21
【问题描述】:
class dbConnection {
function connect(){
try{
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user, $this->db_pass);
return $this->db_conn;
} catch(PDOException $e) {
return $e->getMessage();
}
}
}
class student{
public $link;
public function __construct(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
return $this->link;
}
public function checkLogin($username,$password){
$query = $this->link->prepare("SELECT * FROM studentprofiles where UserName = :uname AND LogPassword = (select md5(:upassword));");
$query->execute(array(':uname' => $username, ':upassword' => $password));
$count = $query->rowCount();
if($count === 1){
$this->setSession($username);
}
return $count;
$query = null;
}
public static function display(){
$query = $this->link->prepare("SELECT ForeName, Surname FROM studentprofiles where UserName = :uname;"); //getting error here: Fatal error: Using $this when not in object context
$query->execute(array(':uname' => self::getSession()));
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
printf (" <span id='WelcomeName'> Welcome: %s %s\n </span>",$row[0],$row[1]);
}
$query = null;
}
}
再次使用 $this 准备另一个 select 语句时出错,我如何再次将它用于同一类中的另一个函数?谢谢你
感谢任何帮助,真的卡在这个问题上
【问题讨论】:
-
最后一个语句的重复应该是什么意思?
-
你遇到了什么错误?
-
如果你使用
PDO::FETCH_ASSOC,你需要使用$row['ForeName']和$row['Surname']来访问行元素。如果你想使用$row[0],你必须使用PDO::FETCH_NUM。 -
致命错误:当不在对象上下文中使用 $this - $query = $this->link->prepare("SELECT ForeName, Surname FROM studentprofiles where UserName = :uname;");
-
那是因为
display方法是静态的。静态方法中没有$this对象。
标签: php mysql pdo this prepare