OOP使用php和mysqli从数据库返回数据

时间:2015-02-12 18:59:09

标签: php oop mysqli

我有一个数据库php文件和一个用户php文件。数据库是我连接数据库的地方。

<?php
    /*
     * Mysqli database class - only one connection
     */
    class Database{
        private $_connection;
        private static $_instance; //The single instance
        private $_host = 'localhost';
        private $_username = 'root';
        private $_password = '';
        private $_database = 'blogwebsite';

        public static function getInstance(){
           if(!self::$_instance){ // If no instance make one
                self::$_instance = new self();
            }
            return self::$_instance;
        }

        /*
         * Constructor
        */
        public function __construct(){
            $this->_connection = new mysqli($this->_host, $this->_username,  
            $this->_password, $this->_database);

             // Error handling
             if(mysqli_connect_error()){
             trigger_error("Failed to connect to MySQL: " .  
              mysqli_connect_errno(), E_USER_ERROR);
           }else{
               echo "You are connected to " . $this->_database;
           }
       }

        // Magic method clone is empty to prevent duplication of connection
        public function __clone(){ }

       // Get mysqli connection
       public function getConnection(){
           return $this->_connection;
        }
    }
?>

这是用户页面,我包括数据库页面。我收到的错误是说&#39;未定义的变量:mysqli&#39; AND&#39;在非对象上调用成员函数prepare()。

<?php
    include('class.database.php');

    class api{

       public function __construct(){
           $db = Database::getInstance();
           $mysqli = $db->getConnection();   
       }

       public function getUsername($username){
           $statement = $mysqli->prepare("SELECT username FROM users");
           $statement->bind_param('s', $username);
           $statement->execute();
           $statement->bind_result($username);
           $statement->fetch();
           $statement->close();

           return $username;
        }
    }

    $username = new api();

    echo $username->getUsername($username);
?>

任何人都可以请求任何帮助。非常感谢你。

2 个答案:

答案 0 :(得分:0)

你在'api'课程中没有'mysqli'变量。此外,您应在获取getConnection()实例后致电Database

class api{       
       private $mysqli;
       public function __construct(){
           $db = Database::getInstance()->getConnection();
           $this->mysqli = $db->getConnection();   
       }

       public function getUsername($username){
           $statement = $this->mysqli->prepare("SELECT username FROM users");
           $statement->bind_param('s', $username);
           $statement->execute();
           $statement->bind_result($username);
           $statement->fetch();
           $statement->close();

           return $username;
        }
    }

答案 1 :(得分:0)

您需要创建一个局部变量$ mysql:

class api{
       protected $mysql; // <-- Here

       public function __construct(){
           $db = Database::getInstance();
           $this->mysqli = $db->getConnection();   
       }

       public function getUsername($username){
           $statement = $this->mysqli->prepare("SELECT username FROM users");
           $statement->bind_param('s', $username); // <-- what this for ?
           $statement->execute();
           $statement->bind_result($username);
           $statement->fetch();
           $statement->close();

           return $username;
        }
    }

    $username = new api();

    echo $username->getUsername($username);