Why did replacing all my "mysql" with "mysqli" stop my project?

Asked

Viewed 7,802 times

1

What should I do? I must configure in the database?

Before everything worked normally, user registration, login...

<?php
   $host ="localhost";
   $user ="root";
   $pass ="senha"; 
   $banco="usuarios";

   $conexao = mysqli_connect($host, $user, $pass) or die (mysqli_error());
   mysqli_select_db($banco) or die (mysqli_error()); 
?>
  • Any error message appears? the mysqli extension is enabled? could you put your code.

  • No message appears, but the login form disappears when I click send.

  • Where do I enable this extension?

  • 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

  • No, the background stays. I’ll try that and tell you.

  • 1

    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 !!!

  • My connection is like this:

  • Insert the code that is giving error in the question.

  • 2

    Moved to mysqli wanting to improve security, only this is not enough. You need to use Prepared statements.

  • <?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(); ?>

  • Right, but beyond security, there’s the fact that it’s obsolete, so I want to use the most current.

  • 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".

  • 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_.

  • Thank you, I’ll read it right now.

  • 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.

  • By your code, mysqli_select_db need two arguments the first the connection and the second the name of the bank

  • But how did it work before? I checked my php and mysql are all version higher than 5. I’m reading something here.

Show 12 more comments

3 answers

4

Here I present according to the documentation one of the exact ways of working with mysqli_ which is quite different from the primary consultations of mysql. First, you can already write the variables directly within the instruction as follows and apply the demonstrated method: Make the connection and store it within a variable.

// Conexão com o banco de dados
$conecta = mysqli_connect("host", "usuario", "senha", "banco") or die("Error " . mysqli_error($conecta)); 

// Faça então qualquer operação do CRUD seguindo as diretivas abaixo: 
$consulta = "SELECT * FROM usuarios" or die("Error in the consult.." . mysqli_error($consulta)); 

// Execute a consulta 
$resultado = $conecta->query($consulta); 

This is just the basis for working with mysqli_ and you also have another option that is PDO. I’m also adding a while if you need to return data for comparison:

while($linha = $resultado -> fetch_assoc()){
    echo '<p>'.$linha["nome"].'</p>';
    echo '<p>'.$linha["cpf"].'</p>';
    // etc ...
}; 
  • 1

    Let me get this straight, I’m gonna have to redo my code? It is not only changing "mysql" by "mysqli", but the structure to, for example, launch data into the database and recover them is a little different?

  • 2

    Yes because it’s a different architecture.

  • 2

    Redoing the code is essential when it comes to security and efficiency. I would recommend PDO, is the kind of change needed that is worth much only.

4

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...

2

It is necessary to pass two arguments to mysqli_select_db(), the first the connection and the second the name of the bank, as suggested in link passed by @marcosvinicius

mysqli_select_db($conexao, $banco);

Or make the connection by passing the bank name in this way:

$conexao = mysqli_connect($host, $user, $pass, $banco);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.