0

I've been getting these two error messages. my code is

public function __construct() {

        $db->connect();

    }

    function connect() {

        return  new mysqli('localhost', DB_USER, DB_PASS,DB_NAME);


    }

Does anyone know what my issue is. i am still very new to PHP so sorry if this is a stupid question.

my DB_USER ,DB_PASS, DB_NAME are saved in my config file

  • I've worked out the first error i had -> instead of = but i am still getting a Fatal error: Call to undefined function connect() in – BeginnerJavaDev Jul 06 '15 at 10:42
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Jul 06 '15 at 17:26

1 Answers1

1

You need to use $this -

$this->db->connect();

$db supposed to be a member variable of that class. You can' access $db like that unless you have passed it to the function like -

function your_function($db) {

To access the member variables $this pointer is used, which points to the current instance.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • thank you. tbh i've been trying to use a old open source DB classed that used mysql not mysqli- and just had loads of problems. so think i am going to have to write it all out myself – BeginnerJavaDev Jul 06 '15 at 11:14
  • That will be better to undrstand. – Sougata Bose Jul 06 '15 at 11:15
  • you wouldn't happen to know a good up to date tutorial? my goal is to build a class that connects to the database - using prepared statements inserts - updates - selects- all in a general way so the class can be used in multiple projects. then i want to create two custom classes that call on the methods of the db class to insert /update/select with the unique queries for this application. i've found a few but any recommendation from someone who knows abit more about what there doing. would be greate – BeginnerJavaDev Jul 06 '15 at 11:27