Functions and php include

When making all my functions stored in a separate file, do I need to include the db connection file inside each function or is it enough to include it once on the functions page or is there a more tidy way to do it.?

I know there is one function that don’t work if I dont include it inside the function

if you can show us your code it would make it much easier to help you out. But yes you are correct you should only need to include the file once.

I’m pretty sure this was one that didnt work without

function getContent($dbQuery) {
	require 'connection.php';
	
	$query = $dbQuery;
	$sql=$oDB->prepare($query);
	$sql->execute();
	//$row = $sql->fetchAll();
	$row = $sql->fetchAll(PDO::FETCH_ASSOC);

	return $row;
}

It is extremely sloppy to have to include connection.php in every function that needs to access the database. The database connection should be created outside the functions and passed as an argument into functions which require the database connection.

Also if I were a betting man I would bet that connection.php creates a new database connection each time it is required. In the typical application you will only want to create the connection once and use that same instance throughout the entirety of the request lifecycle.

tldr;
Database connection in separate file:

        <?php
        $connection = mysqli_connect('localhost', 'root', 'password', 'database name');
            if (!$connection) {
                die("database connection failed");
            }
        ?>

And code referencing it

    <?php include "database.php" ?>
    <?php
        function FunctionName() {
            global $connection;
    function code here;
    }
?>
1 Like

I intentionally left out globals because they should never be used. Using globals are bad practice resulting in code difficult to test and maintain.

Can you explain more why please?

If this isn’t enough

maybe the possible introduction of security vulnerabilities on top of hard to debug broken code would convince you that GLOBALs are a bad habit that should be used only when absolutely necessary and not even then.

What hard to debug broken code are you referring to? Do you mean in general?

What is the alternative to not using a GLOBAL (EDIT:) for a connection?

@AirFor, look, most PHP users confuse the real disastrous usage of this operator with rather innocent use case.

What is a real sin and should be always avoided is tossing local variables between functions like this:

function1() {
  global $var;
  $var = 1;
}
function2() {
  global $var;
  echo $var;
}
function1();
function2();

this should be avoided at any cost, and always written as

function1() {
  $var = 1;
  return $var;
}
function2($var) {
  global $var;
  echo $var;
}
$var = function1();
function2($var);

While for a really global service it is no harm to use it. if it’s sort of constant that have to be globally available - why not to use global then?

But so strong is a superstitious disgust to globals, that people tend to avoid it at any cost, and reinvent wheels instead, with same meaning, but different look.

Well, the above has a touch of trolling. Even real global variables still bear some drawbacks.

First, they are writable. Means in some part of code you may accidentally set it to null and then wondering, why it stopped working elsewhere.

Another problem is that sometimes you need several services of the same kind, say, more than one db handler. One single global variable is not that flexible. That is why it is generally recommended to send all function variables as parameters - this way you will have more control on them, injecting the exact srrvice you need in this very function call.

Well, back to connection. Please take a look at the suggestion I made in the recent thread on the same topic

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.