0

[SOLVED] I have solved it by calling database connection from dbfunction.php instead of creating an object of creating object of dbconnect.php. like this.

 function __construct() {
     require_once('database/config.php');
 $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
 } 

I am trying to insert data to database using mysqli. But it always shows me error

Notice: Undefined variable: conn in D:\xampp\htdocs\fokrulcart\class\dbfunction.php on line 33 Fatal error: Uncaught Error: Call to a member function query() on null in D:\xampp\htdocs\fokrulcart\class\dbfunction.php:33 Stack trace: #0 D:\xampp\htdocs\fokrulcart\reg.php(11): dbFunction->reg('re ay', 'aer yt', 'aetu') #1 {main} thrown in D:\xampp\htdocs\fokrulcart\class\dbfunction.php on line 33

Here is my connection:

<?php  
class dbConnect {  

    function __construct() {  
        require_once('config.php');  
        $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);  
        if(!$conn)
        {  
            die ("Cannot connect to the database");  
        }
        else{
            echo "Connected";
            return $conn; 
        }  

    }
}  
?>  

Here is my page from where I am sending data to dbfunction.php page.

<?php 
include 'include/header.php';
include 'class/dbfunction.php';
$funObj = new dbFunction(); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $f = $_POST['firstname'];
    $l = $_POST['lastname'];
    $e = $_POST['email'];
//echo $f. $l.$e;
    $r = $funObj->reg($f,$l,$e);
}
?>

And this is my dbfunction.php page. Error shows for this page.

<?php  
require_once 'database/dbConnect.php';  

class dbFunction {  
    public $db;
    public $conn;
    function __construct() {  
        $conn = new dbConnect();
    }
    public function reg($l, $f, $e){
        $sql = "INSERT INTO reg (f, l, e)
        VALUES ('John', 'Doe', 'john@example.com')";
        if ($conn->query($sql) === TRUE) { //error shows here.
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
Fokrule
  • 844
  • 2
  • 11
  • 30
  • 2
    Ehm, $conn in dbfunction.php is not public, if you are assign it from the constructor you have to reference to it with the special word $this, so you have to change your code with $this->conn = new dbConnect(); – Lucarnosky Nov 03 '17 at 15:22
  • Remove the `echo "Connected";` in `echo "Connected"; return $conn;` at the end of your `class dbConnect`. – Sean Nov 03 '17 at 15:25
  • 1
    Note: constructors can't return things. If you return something in a constructor, it will be ignored. You'll only ever get a new instance of the object. (Not sure why trying to do so doesn't generate a warning.) – Alex Howansky Nov 03 '17 at 15:25

2 Answers2

4

There are two problems here:

  1. You cannot return anything from the constructor so new dbConnect() will never give you a mysqli object. It will give you a dbConnect so you would need to store your database connection in that class and provide a method to get it.
  2. When you set a property of an object, you need $this, so $this->conn = new dbConnect;
jeroen
  • 91,079
  • 21
  • 114
  • 132
3

In your code the variable $conn is only defined in the __construct function. You should write the value to the global variable by

function __construct() {  
     $this->conn = new dbConnect();
}

And then to call the global variable use

$this->conn->query($sql)
Pascal
  • 413
  • 3
  • 13