【发布时间】: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');,我关注了 this,this
& 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->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
【问题讨论】:
-
@Naruto 我关注了,但没有为我工作
-
请告诉我为什么不赞成,以便我纠正它.....
-
我们已经告诉你聊天中的问题了。请不要滥用 Stack Overflow 作为实时调试工具。至少试着去理解别人现在已经多次告诉你的错误。
-
@Gordon 我关注了该链接并声明为
$DB_con,但它对我没有用 -
请检查更新后的问题@Gordon 我声明为
global $DB_con;但仍然不适合我......
标签: php fatal-error