-2

i am getting error : Fatal error: Using $this when not in object context in line $stmt_edit = $this->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid'); , i followed this , this & this and all other google , yahoo links but nothing worked for me, please check below code and help me.

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

update

i tried by creating new object un user page, but still it did't worked for me : gave this strange error : Fatal error: Call to a member function prepa‌​re() on a non-object in

code

$oDb=new Database();
 $custDb=$oDb->dbConnection();
 $custDb->conn->prepa‌​re('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
7
  • @Naruto i followed , but didt worked for me
    – fresher
    Oct 17, 2016 at 10:37
  • please tell why downvotes, so that i can correct it.....
    – fresher
    Oct 17, 2016 at 10:40
  • 2
    We told you what the issue is in chat already. Please do not abuse Stack Overflow as a live debugging tool. At least try to understand what others have told you now numerous times about the error.
    – Gordon
    Oct 17, 2016 at 10:41
  • @Gordon i followed that link & declared as $DB_con , but it didt worked for me
    – fresher
    Oct 17, 2016 at 10:42
  • please check updated question @Gordon i declared as global $DB_con; but still not working for me......
    – fresher
    Oct 17, 2016 at 10:44

1 Answer 1

0

Your code should be

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 reference to the current object, it's most commonly used in object oriented code.

Reference: http://www.php.net/manual/en/language.oop5.basic.php Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html

and your dbconfig.php should be

<?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;
    }
}
?>
10
  • nowi am getting this error : Fatal error: Call to a member function prepare() on a non-object in for this line : $stmt_edit = $DB_con->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid');
    – fresher
    Oct 17, 2016 at 10:40
  • sorry, i already tried global $DB_con; , but it didt worked for me.....
    – fresher
    Oct 17, 2016 at 10:50
  • you didnt create any object for class Database and you are trying to access its function then it wont work you need to assign connection context to global variable and call method via it. or create new object $oDb=new Database();$custDb=$oDb->dbConnection() and call method like $custDb->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid'); Oct 17, 2016 at 10:53
  • try this $custDb=$oDb->dbConnection(); and call method like $custDb->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid'); Oct 17, 2016 at 10:58
  • dbconfig : pastebin.com/sjV6A1Y9 & editform : pastebin.com/pPezGzmi , now getting error as : Fatal error: Call to a member function dbConnection() on a non-object in looks like i done something wrong ?
    – fresher
    Oct 17, 2016 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.