致命错误:当不在对象上下文中使用$this时,只使用public


Fatal error: Using $this when not in object context in - using public only

我得到错误:Fatal error: Using $this when not in object context in$stmt_edit = $this->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');,我遵循这个,这个,这和所有其他谷歌,雅虎的链接,但没有为我工作,请检查下面的代码和帮助我。

usr

global $DB_con;
 error_reporting( ~E_NOTICE );
 require_once 'dbconfig.php';
 if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
 {
  $id = $_GET['edit_id'];
  $stmt_edit = $this->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
  $stmt_edit->execute(array(':uid'=>$id));
  $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  extract($edit_row);
 }
 else
 {
  header("Location: home.php");
 }

dbconfig

<?php
$db = isset($_POST['db']) ? $_POST['db'] : '';
class Database
{
    private $host = "localhost";
    private $db_name = "designer3";
    private $username = "root";
    private $password = "123456";
    public $conn;
    public function dbConnection()
    {   
    global $DB_con;
    $db = isset($_POST['db']) ? $_POST['db'] : '';
        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

我尝试通过在用户页面上创建新的对象,但仍然没有为我工作:给出了这个奇怪的错误:Fatal error: Call to a member function prepa‌​re() on a non-object in

$oDb=new Database();
 $custDb=$oDb->dbConnection();
 $custDb->conn->prepa‌​re('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');

你的代码应该是

global $DB_con;
 error_reporting( ~E_NOTICE );
 require_once 'dbconfig.php';
 if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
 {
  $id = $_GET['edit_id'];
  $stmt_edit = $DB_con->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
  $stmt_edit->execute(array(':uid'=>$id));
  $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  extract($edit_row);
 }
 else
 {
  header("Location: home.php");
 }

$this对当前对象的引用,它最常用于面向对象的代码中。

参考:http://www.php.net/manual/en/language.oop5.basic.php底漆:http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html

和你的dbconfig.php应该是

<?php
$db = isset($_POST['db']) ? $_POST['db'] : '';
class Database
{
    private $host = "localhost";
    private $db_name = "designer3";
    private $username = "root";
    private $password = "123456";
    public $conn;
    public function dbConnection()
    {   
    global $DB_con;
    $db = isset($_POST['db']) ? $_POST['db'] : '';
        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }
        $DB_con=$this->conn;
        return $this->conn;
    }
}
?>