-1

I am trying to establish a simple connection with mysqli_connect, here is the code (using XAMPP)

$CLASS_CONFIG["table_prefix"] = "TEST";
$CLASS_CONFIG["db_hostname"] = "localhost";
$CLASS_CONFIG["db_user"] = "root";
$CLASS_CONFIG["db_password"] = "";
$CLASS_CONFIG["db_name"] = "test";

$CLASS_CONFIG["debug"] = true;

$CLASS_CONFIG["conn"] = mysqli_connect($CLASS_CONFIG["db_hostname"], $CLASS_CONFIG["db_user"],
                                       $CLASS_CONFIG["db_password"], $CLASS_CONFIG["db_name"]);

However, the mysqli_connect keeps returning NULL. I have tried so many things and scoured the internet and have come up with absolutely no solution!

Thanks for any help

EDIT: This is not a duplicate as no other questions have managed to solve my issue. I am using Laravel and the exact error is:

mysqli_query() expects parameter 1 to be mysqli, null given (View: /opt/lampp/htdocs/lazzaricosmetics/resources/views/admin/admin_prodotti.blade.php)

The query is:

        $result = mysqli_query($CLASS_CONFIG["conn"], $sql) or die (mysqli_error($CLASS_CONFIG["conn"]) . " " . $sql);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Alex Leonardi
  • 71
  • 1
  • 12
  • 2
    http://php.net/manual/en/mysqli.connect-error.php -+- http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Nov 26 '18 at 22:01
  • I will say it again the error is that it returns NULL. There is no error, the connection is supposedly going through. I am using Laravel, the error page says exactly: mysqli_query() expects parameter 1 to be mysqli, null given (View: /opt/lampp/htdocs/lazzaricosmetics/resources/views/admin/admin_prodotti.blade.php) – Alex Leonardi Nov 26 '18 at 22:12
  • I added the laravel tag; could be relevant. – Funk Forty Niner Nov 26 '18 at 22:22
  • @mario you might like to revisit here and see their edit. I added another tag, could be relevant. – Funk Forty Niner Nov 26 '18 at 22:22
  • Thanks, could you perhaps unflag it as duplicate? I fear noboy will check otherwise and I have been at this for way too long. It has to be a silly issue yet I am wasting my entire night on it and I can't get it to work... – Alex Leonardi Nov 26 '18 at 22:23
  • Are you sure you're using the same `$CLASS_CONFIG` variable? Is the place where you perform the query in the same scope as where you set up the connection? – rickdenhaan Nov 26 '18 at 22:24
  • 1
    Then it's probably a duplicate of some other thing. The new error message indicates a scope issue. Still very much impossible to tell without some more debugging details (AKA `var_dump`). – mario Nov 26 '18 at 22:25
  • var_dump just returns NULL I am afraid... – Alex Leonardi Nov 26 '18 at 22:45
  • @rickdenhaan I have one $CLASS_CONFIG array in my config file and declare it as global before trying to use it with "global $CLASS_CONFIG" – Alex Leonardi Nov 26 '18 at 22:47
  • 1
    Does `var_dump($CLASS_CONFIG);` (without `["conn"]`) output `null` right before you call `mysqli_query()`? If so, the variable is not defined. If you are using `global $CLASS_CONFIG` to retrieve it, we need to see a bit more code to figure out where it's going wrong. Specifically, the code leading up to that `mysqli_query()` call. – rickdenhaan Nov 26 '18 at 23:02
  • @rickdenhaan Thank you, I discovered $CLASS_CONFIG contains NULL and don't get why. I fixed it temporarily, badly, by dumping the whole array into $GLOBALS, but why does the global keyword not work the way it should? I have a whole framework given to me which uses global so I want to have it work that way. – Alex Leonardi Nov 27 '18 at 07:25
  • Impossible to say without seeing more code. There are several reasons why this could go wrong, e.g. (incorrect) use of namespaces, wrong order of includes or something as simple as you're (accidentally) unsetting or overwriting it somewhere. – rickdenhaan Nov 27 '18 at 20:14

2 Answers2

0

If you are using the Laravel framework, why do you need to connect to mysql natively?

The mysqli_connect(..) will return null if the connection fails. If the connection is successful, this method will return an Object. You can print error message as follows:

<code>
 if (!$CLASS_CONFIG["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;
}

0

If you are using Laravel you don't have to create the connection to the database by yourself. Laravel does this for you. You have only copy the .env.example file to .env and add you DBMS and DB credentials:

cp .env.example .env

Edit .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=add your db name here
DB_USERNAME=add your db username here
DB_PASSWORD=add your db password here

I encourage you to read the database documenation and Env documentation

common sense
  • 3,775
  • 6
  • 22
  • 31
  • I know it seems weird and I have seen how Laravel does it very easily for you but I am in an awkward situation where I am integrating Laravel with another php-mysql framework so it's all a bit weird. – Alex Leonardi Nov 27 '18 at 15:28
  • May I ask why you doing this? – common sense Nov 27 '18 at 15:31
  • I am developing for a company and they told me to use whatever framework I wanted, after which they told me to use their own framework for the back end of it. I have the entire front end in Laravel's framework and now need to integrate it with their framework for the back end. I could just get rid of Laravel completely but I am on a time constraint so this seemed the best way to go about it. Just disregard Laravel for the back end and keep it as it is for what is already done. Is this a bad idea, do you think I should just get rid of Laravel? I am afraid it would create more problems than not. – Alex Leonardi Nov 27 '18 at 15:36
  • In this case I would create a middleware which exposes the needed data as a (REST) API to one side and interacts with the companies database on the other side. Though I am wondering what Laravel does if it served just as a frontend. Seems a bit heavy to me. With *Middleware* I mean a different application. Not a Laravel Middleware. – common sense Nov 27 '18 at 15:43
  • You are right it is pointless to use Laravel but I did it with the intention of using all of its functionalities but alas, they told me when I was done with the front end to use their framework on the back end – Alex Leonardi Nov 27 '18 at 16:30