William Yang
William Yang

Reputation: 769

PHP5 mysqli vs mysql config files

I know this sounds very stupid indeed, but I find mysqli prepared statements and its object-oriented approach attractive so I plan on converting some of my past doodles from mysql to mysqli.

Here it is... The question

In mysql I used to have a configuration file, for example included in every file that needs the database (i.e. require('sys/dbconfig.php');)

I am not sure if I need to do this in mysqli, or do so as others say, open a connection when you need it, close it when you don't need it (i.e. stick $mysqli = new mysqli($host, $user, $pass, $database); everywhere it needs database transactions.

Upvotes: 3

Views: 2698

Answers (4)

Sliq
Sliq

Reputation: 16494

I do it like that:

config.php

define("DB_HOST", "127.0.0.1");
define("DB_NAME", "demo");
define("DB_USER", "root");
define("DB_PASS", "root");

database.php

// include database constants
include_once("config.php");                   

// create db connection
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);         

now you can make SQL statements via

$result = $db->query("SELECT x FROM y;");            

Upvotes: 1

David Grenier
David Grenier

Reputation: 1241

I tend to create constants in a config file like Panique does.

I also create an App class with a static method fetch_db that contains the code for the connection. Then whenever I need a db connection I just call App::fetch_db().

I can post the code and a little more explanation later this afternoon when I get home.

Upvotes: 0

Mihai Stancu
Mihai Stancu

Reputation: 16117

Mysqli opens the connection when it is instantiated with new mysqli(...) (so does PDO).

During a PHP session you should not need to open/close connections to the database because the duration of a PHP session (for normal websites/webapps) is less than 1 second.

Forcing the equivalent of mysql_connect/mysql_close multiple times in the code can be helpful only in long running scripts that do not need an active connection for their entire duration.

The number of separate mysql connections allowed per mysql user is limited depending on the configuration of you server.

If you have some script that runs a long time and that will likely intersect many other scripts, you may use-up all of the allowed connections per user and thus block other users.

I would suggest (as a simple approach) to instantiate mysqli in a file that you will include everywhere you need db access use the variable you stored the mysqli object everywhere you need it using global $db.

Upvotes: 0

JvdBerg
JvdBerg

Reputation: 21856

MySqlI comes in 2 flavours: procedural and object oriented.

When you use the procedural style everything stays the same as in mysqli_. You are refering to the object oriented style and there the configuration is different .. more object oriented actually. That means that you have to create a new instance of mysqli and use its methods to get the job done.

Note: when using mysqli_ the same way as mysql you will have the same problems with SQL injection. You should use mysqli in combination with prepared queries and parameter binding.

Upvotes: 0

Related Questions