-1

I searched for this, but most of the questions were about mysql/mysqli. I couldn't find out why I get an error which is the next:

Fatal error: Uncaught Error: Call to undefined function connect() in C:\xampp\htdocs\xy\xy.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\xy\xy.php on line 11

The way I got this error message: I have an oop php file with a class in it and within that there's a function (function connect()), this is for database connection.

class dbh {

private $servername;
private $username;
private $password;
private $dbname;

protected function connect(/*$conn*/) {
    $this->servername = "localhost";
    $this->username = "root";
    $this->password = "";
    $this->dbname = "carsale";

    $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
    return $conn;
}

I require the separated php on the main page, and I call the connect() function, but instead of connecting it gives the error and this is not about mysqli.

(I couldn't find any typos.)

Tamás M.
  • 1
  • 4
  • 2
    Show the code where you call `connect` function. Obviously you misunderstand how to call __function__ and how to call __method of class__. – u_mulder Feb 06 '21 at 18:30
  • You need to show the code where the error occurs,,, – Steven Feb 06 '21 at 18:30
  • I won't paste the whole code because it's huge. I just call it like this, connect(); and I tried connect($conn); too. – Tamás M. Feb 06 '21 at 18:41
  • 1
    We **have to** see where you call it... Just show us that line for now – Steven Feb 06 '21 at 18:43
  • It's not a function so you can't call it like a function. This is a method so you can only call it on an object, and because it is protected you can't call it from outside the object. – Dharman Feb 06 '21 at 18:49
  • Now it's just between two php tags, in the body (), because after this I have a form with action (that's why I need the connection). – Tamás M. Feb 06 '21 at 18:51
  • Don't mix PHP and HTML. This will lead to messy code. – Dharman Feb 06 '21 at 18:52
  • First I didn't mixed it, but didn't worked, I had to try if it works this way. – Tamás M. Feb 06 '21 at 18:54
  • 2
    Even if you decide to make it public, then you need to create an object first. You already know how to create objects, because you created one in the code `new mysqli`. You can then call methods of that object, but given that you just use the method to create an object of `mysqli` your whole `dbh` becomes useless. Just remove that class and use mysqli without it or create a proper database abstraction library. – Dharman Feb 06 '21 at 18:54

1 Answers1

0

Make the connect() method public instead of protected

Suleman
  • 104
  • 5