You probably use the native functions in the whole code: mysql_query
, mysql_fetch_array
, mysql_num_rows
, and etc ...
Then to change, for example, from mysql_query
to mysqli_query
, you must also change the arguments, passing the link in the first argument and the query string in the second argument, not just the query string as it was before , and this causes you to have to modify ALL the site queries.
So since you have to change almost everything, I suggest that you make the modifications according to the concept I explain below.
I usually create functions for all DB operations, so when some modification is needed, I only modify my functions and do not need to change anything in the rest of the code, this was very useful when I had to change from mysql_
to mysqli_
, below an example:
include('db_functions.php');
$host ="localhost";
$user ="root";
$pass ="senha";
$banco="usuarios";
jaw_db_connect($host, $user, $pass, $banco);
$cliente_query = jaw_db_query("select * from clientes where email = '" . jaw_db_input($email_address) . "'");
if (jaw_db_num_rows($cliente_query) > 0) {
$cliente = tep_db_fetch_array($cliente_query);
} else {
// nenhum cliente
}
db_functions.php
function jaw_db_connect($server, $username, $password, $database, $link = 'db_link') {
global $$link;
$$link = mysqli_connect($server, $username, $password, $database);
return $$link;
}
function jaw_db_error($query, $errno, $error) {
die('<div><b>' . $errno . ' - ' . $error . '</b></div><div>' . $query . '</div>');
}
function jaw_db_query($query, $link = 'db_link') {
global $$link;
$result = mysqli_query($$link, $query) or jaw_db_error($query, mysqli_errno($$link), mysqli_error($$link));
return $result;
}
function jaw_db_fetch_array($db_query) {
return mysqli_fetch_array($db_query, MYSQLI_ASSOC);
}
function jaw_db_num_rows($db_query) {
return mysqli_num_rows($db_query);
}
function jaw_db_input($string, $link = 'db_link') {
global $$link;
return mysqli_real_escape_string($$link, $string);
}
I just put the main functions above, but just include all the functions that you will use in the site ...
By using this system, you can easily modify the jaw_db_query
function so that it registers in a log file all the querys and the execution time of them, count how many querys were executed on the page, add the total time and write in a variable for you to display in the footer in the development environment, and this helps a lot to identify queries with poor performance, which can cause server overhead ...