You probably use native functions throughout the code: mysql_query
, mysql_fetch_array
, mysql_num_rows
and etc...
So to change, for example, from: mysql_query
for mysqli_query
, you also need to change the arguments, passing the link in the first argument and the query string in the second argument, and not just the query string as it was before, and this makes you have to modify ALL the website querys.
So since you are obliged to modify almost everything, I suggest you already 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 mysql_
for 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 put up only the main functions, but just include all the functions you will use on the site...
Using this system, you can easily modify the function jaw_db_query
for it to record in a log file all the querys and their execution time, count how many querys were executed in the page, add the total time and record in a variable for you to display in the footer in development environment, and this helps a lot to identify low-performance querys, which can cause server overload...
Any error message appears? the mysqli extension is enabled? could you put your code.
– rray
No message appears, but the login form disappears when I click send.
– I Wanna Know
Where do I enable this extension?
– I Wanna Know
Is the screen empty? create a new file and write the following code
<?php phpinfo;
and look for mysqli, if you don’t go to php.ini and unzip the line that has mysqli and restart the server– rray
No, the background stays. I’ll try that and tell you.
– I Wanna Know
I will try to help you ... mysqli_ has to have the following connection to the database:
$link = mysqli_connect("host","user","pass","banco") or die("Error " . mysqli_error($link));
... without it will not work anything else. If you have this ok, queries are different too:mysqli_query($link, "consulta");
... See if this is okay and tell me !!!– Marcos Vinicius
My connection is like this:
– I Wanna Know
Insert the code that is giving error in the question.
– rray
Moved to mysqli wanting to improve security, only this is not enough. You need to use Prepared statements.
– bfavaretto
<?php $host ="localhost"; $user ="root"; $pass ="password"; $bank="users"; $connection = mysqli_connect($host, $user, $pass) or die (mysqli_error(); mysqli_select_db($) or die (mysqli_error(); ?>
– I Wanna Know
Right, but beyond security, there’s the fact that it’s obsolete, so I want to use the most current.
– I Wanna Know
There is no specific code that is giving error, so much so that everything worked normally with "mysql", I just replace all "mysql" by "mysqli".
– I Wanna Know
http://php.net/manual/en/function.mysqli-connect.php ... see instructions on this page because its way of connecting and adding the database is not correct for
mysqli_
.– Marcos Vinicius
Thank you, I’ll read it right now.
– I Wanna Know
I mean, since that’s it, I’ll search and study. The page you sent me is currently unavailable, but your information has already been useful.
– I Wanna Know
By your code,
mysqli_select_db
need two arguments the first the connection and the second the name of the bank– rray
But how did it work before? I checked my php and mysql are all version higher than 5. I’m reading something here.
– I Wanna Know