-1

This is the error I get...

Failed with mysqli_connect()

and also with new mysqli If I put this line in comment "$conn = mysqli_connect..." I don't get the error since it doesn't try to connect to the DB.

Fatal error: Uncaught Error: Call to undefined function Classes\Connection\mysqli_connect() in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php:21 Stack trace: #0 C:\Apache24\htdocs\ControlePM\index.php(22): Classes\Connection\DBConnect->connect() #1 {main} thrown in C:\Apache24\htdocs\ControlePM\Classes\Connection\DBConnect.php on line 21

This is my connection code

I tried with new mysqli same error.

<?php
    namespace Classes\Connection;
    class DBConnect
    {
    var $host;
    var $username;
    var $password;
    var $database;
    public $dbc;
    public $connInfo=FALSE;

    public function __construct(){
        $this->host = 'localhost:3306';
        $this->username = 'root';
        $this->password = 'MyPassWord';
        $this->database = 'MyDB';
    }
    public function connect()
    {
        $conn = mysqli_connect($this->host,$this->username,$this->password,$this->database); 

    if (! $conn) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
    mysqli_close($conn);
    }
?>

Index.php

<?php 
require_once ('Classes/Connection/DBConnect.php'); 
$conn = new DBConnect();
$conn->connect();

if (! $conn->connInfo){
    $result = "Connection failed";
}
else $result = "Connection Succes";
?>

What am I missing?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
David
  • 39
  • 1
  • 1
  • 7
  • You're missing the mysqli extension from your PHP build. – Devon Bessemer Apr 26 '18 at 16:32
  • @Devon The error quite clearly shows that PHP is looking for `mysqli_connect()` in the wrong namespace per `...undefined function Classes\Connection\mysqli_connect() in...`. I am not saying that your suggestion is not valid but it has yet to be proven. – MonkeyZeus Apr 26 '18 at 16:52
  • @MonkeyZeus, PHP will always show the current namespace if the function is undefined (3v4l.org/XbpVd) and the function is undefined because the extension is missing. You don't need to namespace global functions, only if you've defined a local function of the same name. – Devon Bessemer Apr 26 '18 at 18:38
  • Thanks Devon, My mysqli was missing from the PHP build. – David May 28 '18 at 18:24
  • One of those absolutely misleading questions... – Your Common Sense Dec 27 '22 at 06:08

3 Answers3

1

You are using a namespace therefore you'll need to specify that it look for mysqli in the global namespace new \mysqli([...]) otherwise PHP will try and look for mysqli in your current namespace (the one specified in your class).

Secondly, you could explicitly 'import' it via the use statement.

Thirdly, a useful way to check if the mysqli extension is installed is by executing the following var_dump(function_exists('mysqli_connect'));, courtesy of this answer.

Script47
  • 14,230
  • 4
  • 45
  • 66
  • Sorry I omitted to mention that ine my index.php file I am importing use Classes\Connection\DBConnect; Also mysqli is part of the PHP Language Library – David Apr 26 '18 at 17:36
  • 1
    @David read this answer again, and then do it, because this is the correct answer. – Sammitch Apr 26 '18 at 17:39
0

My mysqli was missing from the PHP build

In the PHP.ini file "extension=mysqli" was commented

David
  • 39
  • 1
  • 1
  • 7
-1

for me it was because the way passwords are authenticated with caching_sha2_password in MySQL 8. PHP 7 doesn't support this apparently. But in MySQL 8 you can still store your passwords as mysql_native_password

Try this:

ALTER USER myusername IDENTIFIED WITH mysql_native_password BY 'mypassword';
helvete
  • 2,455
  • 13
  • 33
  • 37