I am using functions to work with database.. Now the way i have defined the functions are as follows:-
/**
* Database definations
*/
define ('db_type', 'MYSQL');
define ('db_host', 'localhost');
define ('db_port', '3306');
define ('db_name', 'database');
define ('db_user', 'root');
define ('db_pass', 'password');
define ('db_table_prefix', '');
/**
* Database Connect
*/
function db_connect($host = db_host, $port = db_port, $username = db_user, $password = db_pass, $database = db_name) {
if(!$db = @mysql_connect($host.':'.$port, $username, $password)) {
return FALSE;
}
if((strlen($database) > 0) AND (!@mysql_select_db($database, $db))) {
return FALSE;
}
// set the correct charset encoding
mysql_query('SET NAMES \'utf8\'');
mysql_query('SET CHARACTER_SET \'utf8\'');
return $db;
}
/**
* Database Close
*/
function db_close($identifier) {
return mysql_close($identifier);
}
/**
* Database Query
*/
function db_query($query, $identifier) {
return mysql_query($query, $identifier);
}
Now i want to know whether it is a good way to do this or not…..
Also, while database connect i am using
$host = db_host
Is it ok? Secondly how i can use these functions, these all code is in my FUNCTIONS.php The Database Definitions and also the Database Connect… will it do the needful for me…
Using these functions how will i be able to connect to database and using the query function… how will i able to execute a query?
VERY IMPORTANT: How can i make mysql to mysqli, is it can be done by just adding an ‘i‘ to mysql….Like:-
@mysql_connect
@mysqli_connect
With global constants, you can directly use them inside a function. So in this case,
And here is how you can implement the client code:
Regarding converting mysql to mysqli, no it might not be that straight-forward; just adding an “i” might not work. MySQLi has different syntaxes. For one, the port is explicitly passed to the connect method rather than appended to the host as done above. Please read the manual.
You should also consider using PDO library as suggested by Simone Vittori.