【问题标题】:How to use PDO prepare more than oncee in a class如何在课堂上多次使用 PDO 准备
【发布时间】: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


【解决方案1】:

您好,您可以检查您的代码,我更喜欢登录以重视连接示例

 $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  

在你的函数中,你做了一个这样的

function connect(){
        try{
            $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
            return $conn;
        } catch(PDOException $e) {
            return $e->getMessage();
        }
    }

在一个完整的例子中:

$id = 5;
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {
        print_r($row);
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

请尝试,祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2020-11-30
    相关资源
    最近更新 更多