Mysqli database connection

Hi,

I was reviewing my code and noticed that at one point I changed how I connected to the database from

$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$db->set_charset('utf8');

to

class DB {
	public static $db;
}
DB::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
DB::$db->set_charset('utf8');

but I don’t remember why I made that change (both works fine). In my application I have a number of functions where I do “select, insert, update” type of database operations.

Is there any practical advantage for using the second way?

Thanks for any ideas.

Looks to me to be the DRY (don’t repeat yourself) principle.
That is, for “one off” use, no negligible advantage,
But if you think you may want to invoke more often, IMHO having a class is the way to go.

[quote=“Mittineague, post:2, topic:198670, full:true”]Looks to me to be the DRY (don’t repeat yourself) principle.
That is, for “one off” use, no negligible advantage,
But if you think you may want to invoke more often, IMHO having a class is the way to go.
[/quote]

Thanks to your comment, now I remember the reason. Since I am using database operations in functions, using a class is more practical so that I will not have to redefine a database connection variable ($db) for each function.

Thank you for the quick help.

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