Make other functions access the $conn variable inside my database connection function
So here I am absolutely desperate trying to make something work. I know what im trying to do is not OOP neither 100% best practice. It is not for a live website, I am just learning some basic PHP concepts on XAMPP.
What I am trying to do is to make the $conn variable inside my database connection function accessible to all other functions that need it. I am thinking of passing it as a parameter, but how can this be done? I prefer not using PHP's "global" or $GLOBALS. My method of working right now is with mysqli using procedural methods.
For example I have something like this:
function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
}
function someFunction () {
$result = mysqli_query ($conn, "SELECT * FROM examples)
}
I never found the answer to my issue...most solutions which I recently got familiar with are OOP based or use somewhat questionable methods.
------------------------------------------------------------------------------------------------------------------------------------
SOLUTION A - I would rather avoid not having my connection in a wrapper and using global variables:
global $conn = mysqli_connect ("localhost", "root", "", "database");
global $conn;
function someFunction () {
global $conn;
$result = mysqli_query ($conn, "SELECT * FROM examples)
}
SOLUTION B - I am not ready for OOP yet but I know this works. The point is I want to learn something different:
class Database
{
private static $conn;
public static function getObject()
{
if (!self::$conn)
self::$conn = new mysqli("localhost", "root", "", "database");
return self::$conn;
}
}
function someFunction () {
$result = mysqli_query (Database::$conn, "SELECT * FROM examples)
}
SOLUTION C - Not using functions at all...just keeping it unwrapped which I dont find very practical in the long term:
$conn = mysqli_connect ("localhost", "root", "", "database");
$result = mysqli_query ($conn, "SELECT * FROM examples)
------------------------------------------------------------------------------------------------------------------------------------
THE SOLUTION I AM TRYING TO ACHIEVE:
function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
return $conn;
}
function someFunction () {
$conn = db ();
$result = mysqli_query ($conn, "SELECT * FROM examples)
}
OR Something like this where I just pass in the connection as a parameter or something (pseudo code at the moment)
function db () {
$conn = mysqli_connect ("localhost", "root", "", "database");
}
function someFunction ($conn) {
$result = mysqli_query ($conn, "SELECT * FROM examples)
}
------------------------------------------------------------------------------------------------------------------------------------
So how do I achieve something like the last two but which actually works. Is this concept possible?
Your Desired Solution: This should work, and you'll only make one connection.
function db () {
static $conn;
if ($conn===NULL){
$conn = mysqli_connect ("localhost", "root", "", "database");
}
return $conn;
}
function someFunction () {
$conn = db();
$result = mysqli_query ($conn, "SELECT * FROM examples);
}
If you used the function someFunction($conn)
, that would make your code much messier, since you wouldn't actually have universal access to $conn
from anywhere.
You should go with Solution B IMO. That way, you can have simple access to it Database::$conn
which will be consistent throughout your script. You could should have an initialize
function (you could use a different name if you want) that will initialize Database::$conn
, and you can then use that to initialize other things on the Database
class later, if desired.
Solution A is terrible. I did that for a long time (global
izing things), and it was a horrible idea. I should have never done that. But I did. And I learned. It just made code get progressively sloppier and sloppier.
Solution B: Database::$conn
should be public
if you want to be able to access it by Database::$conn
from anywhere. If it's private, then you would always need to call Database::getObject();
Solution C: You're right. That would be very impractical.
Solution B rewrite:
class Database
{
/** TRUE if static variables have been initialized. FALSE otherwise
*/
private static $init = FALSE;
/** The mysqli connection object
*/
public static $conn;
/** initializes the static class variables. Only runs initialization once.
* does not return anything.
*/
public static function initialize()
{
if (self::$init===TRUE)return;
self::$init = TRUE;
self::$conn = new mysqli("localhost", "root", "", "database");
}
}
Then... call Database::initialize()
at least once before it gets used.
<?php
Database::initialize();
$result = mysqli_query (Database::$conn, "SELECT * FROM examples);
?>
EDIT
Database::initialize()
immediately after the declaration of the class, in that PHP file. Then initializing is handled.Database::getDb()
than accessing the $conn
property directly. Then initialize
can be called from the getDb()
function. Basically like the Desired Solution but inside a class. The class really isn't necessary, but it can be nice if you like classes, like I do.All of the answers in this section are overkill as they are doing overhead just by creating a wrapper around a database object. For separation of concern(Maintainability) use a separate PHP file for database connection and use require_once.
//Inside Database_Connect.php
$db = mysqi_connect(localhost, database, password);
Now use $GLOBALS['db'] inside your mysqli_ functions wherever needed.
OR, initialize your script / object as
$dbConn = $GLOBALS['db'];
and use $dbConn inside your mysqli_ functions wherever needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With