-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');
Community
  • 1
  • 1
fresher
  • 917
  • 2
  • 20
  • 55

1 Answers1

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;
    }
}
?>
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
  • 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 '16 at 10:40
  • sorry, i already tried `global $DB_con;` , but it didt worked for me..... – fresher Oct 17 '16 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'); – Abhijit Jagtap Oct 17 '16 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'); – Abhijit Jagtap Oct 17 '16 at 10:58
  • dbconfig : http://pastebin.com/sjV6A1Y9 & editform : http://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 '16 at 11:02
  • your full code is here, it should work. $oDb=new Database();$custDb=$oDb->dbConnection();$custDb->conn->prepare('SELECT userName, userEmail, phone FROM tbl_users WHERE userID =:uid'); – Abhijit Jagtap Oct 17 '16 at 11:04
  • i added your code in editform.php file, now i am getting this error : `Fatal error: Call to a member function prepa‌​re() on a non-object in ` – fresher Oct 17 '16 at 11:08
  • editform : http://pasted.co/aeedf016 , dbconfig : http://pasted.co/fe2456d3 – fresher Oct 17 '16 at 11:17
  • $pdo is undefined. You're not declaring it inside the function, and it isn't being passed in as an argument. You need to either pass it in (good), or define it in the global namespace and make it available to your function by placing global $pdo at the top (bad). – Abhijit Jagtap Oct 17 '16 at 11:20
  • i tried this code : `global $conn;` under `public function` , but still it did't worked for me.... – fresher Oct 17 '16 at 11:29