4

How do I allow a function to access a database connection without using GLOBAL?

config.php

 DEFINE ('DB_HOSTNAME', 'hostname');
 DEFINE ('DB_DATABASE', 'database');
 DEFINE ('DB_USERNAME', 'username');
 DEFINE ('DB_PASSWORD', 'password');

 $dbc = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

 if(!$dbc) die("Unable to connect to MySQL: " . mysqli_error($dbc));

functions.php

 function something()
 {
 $info = mysqli_query($dbc, "SELECT info FROM text") or die("Error: ".mysqli_error($dbc));
 }

The above gives me the following error: mysqli_query() expects parameter 1 to be mysqli, null given in

2
  • Let your ART OF DEBUGGING be groomed, Dont put such errors on Stack man Commented Jul 20, 2011 at 9:25
  • 1
    The day you'll change you db to mysql, oracle or another one, you'll get stucked with your connection handler. You should use PDO instead. Commented Jul 20, 2011 at 9:44

5 Answers 5

6

Use function parameters

function something ($dbc) {
  // your db code here
}

function arguments

Sign up to request clarification or add additional context in comments.

2 Comments

That gives me the error: Missing argument 1 for something() and mysqli_query() expects parameter 1 to be mysqli, null given in
Of course you must call the function with the connection handler as the argument!
3

Either pass the database handle to your function, as @KingCrunch and others have said, or call a function that returns the handle:

In config.php:

function get_dbc() {
    $dbc = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if(!$dbc) die("Unable to connect to MySQL: " . mysqli_error($dbc));
    return $dbc;
}

In functions.php:

require_once('config.php');

function something()
{
    $dbc = get_dbc();
    $info = mysqli_query($dbc, "SELECT info FROM text") or die("Error: ".mysqli_error($dbc));
}

You may wish to look at The mysqli Extension and Persistent Connections for details on how you can prevent the connection from being re-established on each call to get_dbc(). There are alternative approaches to this, such as creating a singleton class for your database connection.

Comments

1

There are two ways one is by passing arguments and the other by using function closure like @Ondrej said. But I wonder both of these require you to modify the code if that is the case, then I would suggest you to use global keyword.

You can use global keyword to get the scope of variable $dbc

Try this..

function something()
{
   global $dbc;
   $info = mysqli_query($dbc, "SELECT info FROM text") or die("Error: ".mysqli_error($dbc));
}

(OR)

Try this...

function something()
{
    $dbc = func_get_arg(0);
     $info = mysqli_query($dbc, "SELECT info FROM text") or die("Error: ".mysqli_error($dbc));
}

& do this ....

$query = something($dbc);

1 Comment

He doesn't want to use global.
1

its so simple just pass your $conn variable into another calling function(instead of making new connection) like

yourpage.php

$conn = new mysqli($servername, $username, $password, $dbname);
someFunction ($conn)//you can add other parameters if you like 

function someFunction ($conn) {
    $result = mysqli_query ($conn, "SELECT * FROM examples);
}

Note:This is not good practice to always make new connection for database access.so always make connection once and use it every where.(but if your requirement different and require multiples connections then you can make multiples connections)

Comments

0

There are more ways. You could use classic procedural style:

function something($dbc)

or anonymous function (if you use PHP5.3):

$fn = function() using ($dbc)

1 Comment

I just showed possibilities. And anonymous function is a possibility in this case - he can use using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.