1

Is there any idea how to import mysqli connection from the include file?

db_connect.php

<?php
class DB_CONNECT {
    /* -- */
    function connect() {
        require_once __DIR__ . '/db_config.php';
        $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_connect_error());
        $db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_connect_error()) or die(mysqli_connect_error());
        return $con;
    }
    /* -- */
}

index.php

<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysqli_query($db, "SELECT *FROM tasks");

If I do this, will get this error message.

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in index.php on line XXX

halfer
  • 19,824
  • 17
  • 99
  • 186
Nurdin
  • 23,382
  • 43
  • 130
  • 308
  • What is the result of `var_dump($db);`? – GROVER. Sep 08 '19 at 05:08
  • 1
    You don't need `mysqli_select_db()`. You can do `mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE)` with constant from your config file. – EternalHour Sep 08 '19 at 05:27
  • 1
    The proper answer to this question would explain that you don't need the class you have created. You should also enable MySQLi exceptions, because your current code is leaking sensitive information. If you want to use OOP then you should use it for MySQLi too. – Dharman Sep 08 '19 at 05:41

3 Answers3

4

Given DB_CONNECT class doesn't really add any value, just get rid of it, and simply put a proper connection code into db_connect.php, and then just include this file in your scripts. Mysqli is a class already, no need to add another one without any added value. You can create a $db variable out of mysqli class as well.

First, write a proper mysqli connection code (my article dedicated to the matter, I highly recommend to read it as it explains many things that are wrong or missing in your current code):

require_once __DIR__ . '/db_config.php';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $db = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
    $db->set_charset($charset);
} catch (\mysqli_sql_exception $e) {
     throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}

And then just include this file in other scripts: a $db variable would be already there:

<?php
require_once __DIR__ . '/db_connect.php';
$response = $db->query("SELECT *FROM tasks")->fetch_all(MYSQLI_ASSOC);

see - just two lines!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

You should be using the function of that class instead of using the object of it, As the connection you intend to create can be done by returned $con variable in that function and not the object itself.

$conn= $db->connect();

$result = mysqli_query($conn, "SELECT *FROM tasks");

EDIT : Creating $conn in query statement is not a good thing as mentioned by @YourCommonSense and @dharman

Mukul Kumar Jha
  • 1,062
  • 7
  • 19
2

$db = new DB_CONNECT(); invokes the object.

You need to call:

$con= $db->connect();

And then use $con as:

  $result = mysqli_query($con, "SELECT *FROM tasks");
Sachin Bahukhandi
  • 2,378
  • 20
  • 29