I have two files, one called connection.php and one called suggest.php, in connection.php I do the connection perfectly with a function called connectDB and I use it without any problem in suggest.php.
However, I have another function in which I want to display the information from my database with a SELECT, and I would like to do it through a function as well. The error that PHP throws me is the following:
Fatal error: Call to a member function query() on null in C:\AppServ\www\object\db\inc\connection.php on line 18
I am not very expert but I think it has to do with the scope of the variable, since within the connectDB function there is the variable (object) $db.
Can I make that variable global so I can use it in all functions? How can I do it?
suggest.php
<?php
include 'connection.php';
connectDB('root','administrador');
selectMedia();
?>
connection.php
function connectDB($user, $pass, $dsn = 'mysql:host=localhost;dbname=database'){
try {
$db = new PDO($dsn, $user, $pass);
$db->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
} catch (Exception $e) {
echo "Error: " . $e->getMessage()."<br>";
echo "Código de excepción: " . $e->getCode()."<br>";
echo "Línea fuente: " . $e->getLine()."<br>";
echo "Archivo: " . $e->getFile();
}
}
function selectMedia(){
try {
$results = $db->query("SELECT title, category FROM Media");
echo "Cool!";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
}
?>
Creating a variable
global
is not a good practice / Globals are evilYou can create a file
global
where you store for example all your essential functions for your application, in this case it would be for example the configuration to connect to your databaseconnectDB(...)
.connection.php
Once the file is created,
global
you include it in any file where you always need it.anyFile.php
A single dimension to the solution proposed by @aldanux, if your connection.php file is called in several files, I recommend you use include_once
This does exactly the same thing as a normal include, but if your file was already included, it doesn't include it again, this will avoid errors due to variables already defined.
More elegant would be to create a class, in a separate .php, and use the singleton pattern to have a single instance of the object you want to create (in this case your supposed global variable) so that you can always access it from anywhere and that is always instantiated and with a unique value for the entire project. I leave you a link to guide you: https://www.uno-de-piera.com/el-patron-singleton-en-php/
Maybe you are complicating yourself in vain? I think you would solve everything with a simple return in your function:
Something simpler and less complication, above all we get a
function
cleaner is to create a config.ini.php fileAnd in the PHP document that requires database connections, call them via
include_once
Inside the file you create the connection, imagining that the variable
$con
has the values of the connection.It would only suffice to add one of the PHP
global
reserved words to thefunction
Example: