Why when replacing all my "mysql" with "mysqli" did my project stop working? [closed]

1

What should I do? Should I configure it 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()); 
?>
    
asked by anonymous 17.08.2014 / 18:41

3 answers

4

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

    
17.08.2014 / 20:59
4

Here I present according to the documentation one of the exact ways of working with mysqli_ which is quite different from the primary queries of mysql . First, you can already write the variables directly inside the statement as follows and apply the demonstrated method: Make the connection and store it inside 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 only the basis for working with mysqli_ and you also have another option that is PDO . I'm also adding 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 ...
}; 
    
17.08.2014 / 19:10
2

It is necessary to pass two arguments to mysqli_select_db() , 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 name of the bank this way:

$conexao = mysqli_connect($host, $user, $pass, $banco);
    
17.08.2014 / 19:11