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

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'); ,我跟着 this , this & 和所有其他 google ,雅虎链接但对我没有任何作用,请检查下面的代码并帮助我。

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;
    }
}
?>