• <bdo id='E7xN9'></bdo><ul id='E7xN9'></ul>

      <legend id='E7xN9'><style id='E7xN9'><dir id='E7xN9'><q id='E7xN9'></q></dir></style></legend>
    1. <i id='E7xN9'><tr id='E7xN9'><dt id='E7xN9'><q id='E7xN9'><span id='E7xN9'><b id='E7xN9'><form id='E7xN9'><ins id='E7xN9'></ins><ul id='E7xN9'></ul><sub id='E7xN9'></sub></form><legend id='E7xN9'></legend><bdo id='E7xN9'><pre id='E7xN9'><center id='E7xN9'></center></pre></bdo></b><th id='E7xN9'></th></span></q></dt></tr></i><div id='E7xN9'><tfoot id='E7xN9'></tfoot><dl id='E7xN9'><fieldset id='E7xN9'></fieldset></dl></div>

      <small id='E7xN9'></small><noframes id='E7xN9'>

    2. <tfoot id='E7xN9'></tfoot>

      致命错误:调用未定义的方法 Database::prepare()

      时间:2023-10-05
          <tfoot id='zI5Kn'></tfoot>
            <tbody id='zI5Kn'></tbody>

          1. <i id='zI5Kn'><tr id='zI5Kn'><dt id='zI5Kn'><q id='zI5Kn'><span id='zI5Kn'><b id='zI5Kn'><form id='zI5Kn'><ins id='zI5Kn'></ins><ul id='zI5Kn'></ul><sub id='zI5Kn'></sub></form><legend id='zI5Kn'></legend><bdo id='zI5Kn'><pre id='zI5Kn'><center id='zI5Kn'></center></pre></bdo></b><th id='zI5Kn'></th></span></q></dt></tr></i><div id='zI5Kn'><tfoot id='zI5Kn'></tfoot><dl id='zI5Kn'><fieldset id='zI5Kn'></fieldset></dl></div>

              <small id='zI5Kn'></small><noframes id='zI5Kn'>

                <bdo id='zI5Kn'></bdo><ul id='zI5Kn'></ul>
                <legend id='zI5Kn'><style id='zI5Kn'><dir id='zI5Kn'><q id='zI5Kn'></q></dir></style></legend>
                本文介绍了致命错误:调用未定义的方法 Database::prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我为数据库和用户创建了一个单独的类.

                I have created a separate class for database and users.

                数据库.php

                 class Database{
                
                     private $db;
                
                
                    public function __construct(){
                
                  /*** mysql hostname ***/
                $hostname = 'localhost';
                
                /*** mysql username ***/
                $username = 'username_web';
                
                /*** mysql password ***/
                $password = 'password_web';
                
                
                
                try {
                    $this->db = new PDO("mysql:host=$hostname;dbname=kamadhenu_web", $username, $password);
                    /*** echo a message saying we have connected ***/
                
                       }
                catch(PDOException $e)
                    {
                    echo $e->getMessage();
                    }
                
                 }
                
                  /*** Query Function ***/
                 public function query($sql)
                        {
                        return $this->db->query($sql);
                        }
                
                
                
                 }
                

                Users.php

                class Users{
                
                     private $db;
                
                public function __construct($database) {
                $this->db = $database;
                
                }
                
                
                     public function login($username, $password)
                     {
                
                        $query=$this->db->prepare("SELECT `password`, `id` FROM `users` WHERE `username` = ?");
                        $query->bindValue(1, $username);
                        try{
                        $query->execute();
                        $data = $query->fetch();
                        $stored_password = $data['password'];
                        $id = $data['id'];
                        #hashing the supplied password and comparing it with the stored hashed password.
                        if($stored_password === sha1($password)){
                        return $id; 
                        }else{
                        return false;   
                        }
                
                        }catch(PDOException $e){
                        die($e->getMessage());
                }
                
                 }
                
                
                
                
                 }
                

                这是我的登录页面,包含用户名和密码.

                Here is my Login page with username and password.

                 login.php
                
                include('database.php');
                include('users.php');
                
                $dbh= new Database();
                $users= new Users($dbh);
                
                
                if (isset($_POST['submit']))
                
                { 
                
                $username= $_POST['username'];
                $password= $_POST['password'];
                
                    $login = $users->login($username, $password);
                
                
                
                
                        if ($login === false) {
                        $errors[] = 'Sorry, that username/password is invalid';
                        }
                        else {
                        // username/password is correct and the login method of the $users object returns the user's id, which is stored in $login.
                
                        $_SESSION['id'] = $login; // The user's id is now set into the user's session in the form of $_SESSION['id']
                        #Redirect the user to home.php.
                        header('Location: list-updates.php');
                        exit();
                        }
                
                
                
                
                
                }
                

                执行时出现错误:

                调用未定义的方法 Database::prepare()

                Call to undefined method Database::prepare()

                推荐答案

                您在实例化 Database() 时创建了 $dbh,但是实例化 Database 只会返回您的 Database 类的实例,而不是您的数据库连接.您应该有一个 getDb 来从数据库对象获取连接:

                You create $dbh when you instantiate Database(), but instantiating the Database only returns an instance of your Database class, not your db connection. You should have a getDb to get your connection from database object:

                $dbClass = new Database();
                $dbh = $dbClass->getDb(); // here you get the connection
                $users= new Users($dbh);  // here you give to Users() the $dbh, that isn't your 
                                          // connection.. it's just Database class
                

                数据库构造仅返回数据库类的实例,而不返回数据库连接

                class Database{
                
                 private $db;
                
                
                public function __construct(){
                
                    try {
                     $this->db = new PDO("mysql:host=$hostname;dbname=kamadhenu_web", $username, $password);
                    /*** echo a message saying we have connected ***/
                
                   }
                    catch(PDOException $e)
                        {
                            echo $e->getMessage();
                       }    
                 }
                
                 public function getDb() {
                       if ($this->db instanceof PDO) {
                            return $this->db;
                       }
                 }
                
                
                }
                

                这篇关于致命错误:调用未定义的方法 Database::prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:插入值列表与列列表不匹配:1136 列计数与值计数 下一篇:PHP PDO 准备好的语句查询不更新记录

                相关文章

                最新文章

                  <small id='dKqJs'></small><noframes id='dKqJs'>

                  <i id='dKqJs'><tr id='dKqJs'><dt id='dKqJs'><q id='dKqJs'><span id='dKqJs'><b id='dKqJs'><form id='dKqJs'><ins id='dKqJs'></ins><ul id='dKqJs'></ul><sub id='dKqJs'></sub></form><legend id='dKqJs'></legend><bdo id='dKqJs'><pre id='dKqJs'><center id='dKqJs'></center></pre></bdo></b><th id='dKqJs'></th></span></q></dt></tr></i><div id='dKqJs'><tfoot id='dKqJs'></tfoot><dl id='dKqJs'><fieldset id='dKqJs'></fieldset></dl></div>

                    <legend id='dKqJs'><style id='dKqJs'><dir id='dKqJs'><q id='dKqJs'></q></dir></style></legend>
                      <bdo id='dKqJs'></bdo><ul id='dKqJs'></ul>
                    <tfoot id='dKqJs'></tfoot>