0

I have a following code

//dsn.php

//Object Oriented way
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database";

//check connection
$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error) {
    die("could not connect:".$conn->connect_error);
}

//index.php

include 'dsn.php';

function a() {
    $sql = "sql command";
    $result = $conn->query($sql);
    //working

    $conn->close();
}

function b() {
    $sql = "sql command";
    $result = $conn->query($sql);
    //not working

    $conn->close();
}

This will display warning and notice that says:

Warning: mysqli::query(): Couldn't fetch mysqli
Notice: Trying to get property of non-object
Warning: mysqli::close(): Couldn't fetch mysqli

However this one works:

include 'dsn.php';

function a() {
    $sql = "sql command";
    $result = $conn->query($sql);
    //working

    $conn->close();
}

function b() {
    include $dsn.php
    $sql = "sql command";
    $result = $conn->query($sql);
    //working

    $conn->close();
}

How do I use just one include file for DSN and use repeatedly on other functions?

EDIT sorry I forgot to mention

function a($conn) {}
function b($conn) {}

I passed the variable $conn but it still displays the warning and notice I mentioned above

ha_ryu
  • 568
  • 2
  • 7
  • 20
  • not really sure what you mean. But you can do this. In each function make $conn a global variable as in global $conn; – Rotimi Feb 20 '17 at 10:29
  • not sure but I also think that closing the connection must require a reconnect before another operation, so if you close $conn in a(); than you have to reconnect before the query in b(). Isn't it? – utnaf Feb 20 '17 at 10:31

2 Answers2

1

When you include a file, you can imagine that in the background it is just copy-pasting that code into the current document.

There are 2 problems with your code...

  1. The $conn variable is not in scope inside function a or b.
  2. Even if it was in scope and accessible, you are closing the connection after each query. A better way to do it is to open the connection, run all queries and close the connection when it is no longer needed.

The second piece of code you gave works because it is creating a new variable $conn inside of b(), but this is not ideal as it will create a new database connection every time you execute that function.

Something like this may suit your needs:

include 'dsn.php';

function a($conn) {
    $sql = "sql command";
    $result = $conn->query($sql);
    return $result;
}

function b($conn) {
    $sql = "sql command";
    $result = $conn->query($sql);
    return $result;
}

$aResult = a($conn);
$bResult = b($conn);

$conn->close();

Notice that we are only including 'dsn.php' once, and then passing around that existing connection to the functions that need it.

Tom Wright
  • 2,841
  • 4
  • 22
  • 30
-1

This is very simple. On page load, the connection file is included which makes $conn the connection object available to the remaining codes. The $conn is used by the functiona() and it is then closed at the end of the function. $conn->close(); destroys the database connection object means, $conn is no more object hence it should not be treated as object. But the Function b() is treatong it as database connection object and resulting into error.

But if you again include the connection file, inside the function b() then $conn becomes available to the function as local object. And works as it should.

Do not close the $conn() on the any function till you are dealing with DB.

function a() {
   incDbConnectionFile();
   $sql = "sql command";
   $result = $conn->query($sql);
   //working

   $conn->close();
}

function b() {
   incDbConnectionFile();
   $sql = "sql command";
   $result = $conn->query($sql);
   //working

   $conn->close();
}


function incDbConnectionFile() {
  include 'dsn.php';
}
Shubhranshu
  • 511
  • 3
  • 12
  • thanks for the info, so you mean to say I should keep opening the database? is there any way to close the db on each functions? – ha_ryu Feb 20 '17 at 10:43
  • If you explore any method, then the connection file is every time included, whether it is made through connection class. hence you have to include file every time. But you can get rid of with this using the above corrections. – Shubhranshu Feb 20 '17 at 10:50
  • @Shubhranshu Your example will still include the file every time... You're just doing it through a function call rather than including it directly. Depending on how this is used, it could be creating and closing hundreds of connections rather than just using a single connection and closing it when you are done with the application. – Tom Wright Feb 20 '17 at 14:07