简体   繁体   中英

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 (ie 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 (ie stick $mysqli = new mysqli($host, $user, $pass, $database); everywhere it needs database transactions.

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;");            

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM