I have created a database connection instance in PHP like:
$mysqli = mysqli_connect("localhost", "root", "blabla", "blabla");
I need to pass this database connection to other PHP files, since I have an HTML form that first inserts data into a database and then it goes to a PHP page that retrieves data.
I know that I must not instantiate the database connection each time but I do not know how to do it, because I am not so familiar with OO PHP.
You can just name a file connection.php and store your line for connection to database. Then you can include your file like this :
require_once('connection.php');
This will be the content of connection.php :
$mysqli = mysqli_connect("localhost", "root", "blabla", "blabla");
And then this way, your file will never load twice.
If $mysqli
is global, then all php files can see it by peeking the $GLOBALS
array, assuming the php is included.
For example:
$mysqli = mysqli_connect(..); // in global space
another php file:
$GLOBALS["mysqli"]; // use that
inside a php function you could also do:
global $mysqli; // use that
If I understand you correctly, I think you misunderstand what you've read.
In PHP how most systems (phpBB, WordPress, etc) do it is there is a base include file that is included at the start of each PHP page. In phpBB it is called "Common.php" if I remember correctly.
Common.php goes through and does a few things:
This is done via one of 4 functions:
include('common.php')
will include the contents of the file at the point it is written in the file. E.G.
<?php
do_function();
include('common.php');
do_other_function();
?>
Will run do_function(), then run any script in the common.php file, which could theoretically hold the definition for do_other_function(), and then run do_other_function();
include_once('common.php')
makes sure that a file is run through the process only once. This is good for things like class definitions as it ensures a class is not accidentally re-defined. E.G.
<?php
include_once('database_class.php');
//Code here code here
...
//Oh crap, did I include that database class? I can't remember. I think so...maybe not
include_once('database_class.php');
?>
Will only include the database class def one time. Where as the same example above with include('database_class.php');
instead of include_once
would throw an error akin to the class 'database' is already defined
Note, include and include_once will only throw warnings if a file does not exist, and if you have error reporting set to only show fatal errors, you won't notice when something isn't included except for perhaps a "constant/function/class 'something' not defined" and you'll scratch your head for perhaps a long time. So if you try to include the file 'cmmon.php' when you meant 'common.php', you may or may not see an error.
then there is the require
and require_once
functions, they do effectively the same thing as include
and include_once
, but unlike the latter two the script will stop running if it cannot include the file and throw an error, like "could not include file 'something.php' on line # [line] in file [file path]"
Use require for scripts that are absolutely necessary - function/class/constants that your scripts will need in order run properly.
Use include for things that are voluntary, the first thought that came to mind would be plugins - things that don't necessarily have to be there in order for your system to work as intended.