【问题标题】:Fatal error: Using $this when not in object context in - using public only [duplicate]致命错误:不在对象上下文中使用 $this - 仅使用公共 [重复]
【发布时间】:2016-10-17 10:34:42
【问题描述】:

我收到错误: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');,我关注了 thisthis & this 和所有其他 google 、 yahoo 链接但对我没有任何帮助,请检查下面的代码并帮助我。

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");
 }

数据库配置

<?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');

【问题讨论】:

  • @Naruto 我关注了,但没有为我工作
  • 请告诉我为什么不赞成,以便我纠正它.....
  • 我们已经告诉你聊天中的问题了。请不要滥用 Stack Overflow 作为实时调试工具。至少试着去理解别人现在已经多次告诉你的错误。
  • @Gordon 我关注了该链接并声明为 $DB_con ,但它对我没有用
  • 请检查更新后的问题@Gordon 我声明为global $DB_con; 但仍然不适合我......

标签: php fatal-error


【解决方案1】:

你的代码应该是

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

【讨论】:

  • 现在我收到此错误:Fatal error: Call to a member function prepare() on a non-object in 这一行:$stmt_edit = $DB_con-&gt;conn-&gt;prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
  • 对不起,我已经尝试过global $DB_con;,但它对我没有用.....
  • 您没有为类数据库创建任何对象,并且您正在尝试访问其功能,那么它将无法工作,您需要将连接上下文分配给全局变量并通过它调用方法。或创建新对象 $oDb=new Database();$custDb=$oDb->dbConnection() 并调用方法如 $custDb->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid' );
  • 试试这个 $custDb=$oDb->dbConnection();并调用方法如 $custDb->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
  • dbconfig : pastebin.com/sjV6A1Y9 & editform : pastebin.com/pPezGzmi ,现在出现错误:Fatal error: Call to a member function dbConnection() on a non-object in 看起来我做错了什么?
猜你喜欢
  • 2012-09-25
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2010-12-11
相关资源
最近更新 更多