Help with these lines in mysql

-3

Hello I need help with these lines gives me error and I do not know the reason. First the line:

$registro = mysqli_query("SELECT * FROM productos WHERE nomb_prod LIKE '%$dato%' OR tipo_prod LIKE '%$dato%' ORDER BY id_prod ASC");

I get the following error:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in   C: \ wamp \ www \ www \ registration \ php \ product_search.php on line 9

Another line that gives me error:

if(mysqli_num_rows($registro)>0){
    while($registro2 = mysql_fetch_array($registro)){
        echo '<tr>
                <td>'.$registro2['nomb_prod'].'</td>
                <td>'.$registro2['tipo_prod'].'</td>
                <td>S/. '.$registro2['precio_unit'].'</td>
                <td>S/. '.$registro2['precio_dist'].'</td>
                <td>'.fechaNormal($registro2['fecha_reg']).'</td>
                <td><a href="javascript:editarProducto('.$registro2['id_prod'].');" class="glyphicon glyphicon-edit"></a> <a href="javascript:eliminarProducto('.$registro2['id_prod'].');" class="glyphicon glyphicon-remove-circle"></a></td>
                </tr>';
    }
}

In this case the error is because I have to add a parameter. Well it's just that I'm moving from mysql to mysqli which is quite similar

Connection file:

<?php

const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASSWORD = '';
const DB_NAME = 'tienda';
const DB_CHARSET = 'UTF8';

function database_connect()
{
    $conexion=new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
    if($conexion->connect_errno){
        echo "No conectado";
    }else{

    }
}

?>
    
asked by Perl 15.09.2016 в 18:24
source

2 answers

3

The definition of the error comes in the description itself: you are passing a parameter when you should pass two.

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in C: \ wamp \ www \ www \ registry \ php \ product_search.php on line 9

This is because you are using the procedural version of mysqli_query instead of the targeted version to objects, then the first parameter must be the connection to the database and the second the query you want to make:

$registro = mysqli_query($conexion, "SELECT * FROM productos WHERE nomb_prod LIKE '%$dato%' OR tipo_prod LIKE '%$dato%' ORDER BY id_prod ASC");

Where $conexion will be the variable that contains your connection to the database.

    
answered by 15.09.2016 в 18:31
1

To solve the problem, edit your question and add the line of the error that code is found.

To make a query in the database you must add the connection otherwise it will show you errors.

Replaces:

$registro = mysqli_query("SELECT * FROM productos WHERE nomb_prod LIKE '%$dato%' OR tipo_prod LIKE '%$dato%' ORDER BY id_prod ASC");

By:

$registro = mysqli_query($conexion, "SELECT * FROM productos WHERE nomb_prod LIKE '%$dato%' OR tipo_prod LIKE '%$dato%' ORDER BY id_prod ASC");
  

Note The variable $conexion gets the parameters all the data contained in the database connection without it will not result in your query showing you errors.

Read these two topics about making queries with mysqli

Observe in detail in the documentation that the variable of the connection is always added to make queries of the records in the database.

Example:

I create the database connection.

 $mysqli = new mysqli("localhost", "mi_usuario", "mi_contraseña", "world");

/* comprobar la conexión */
  if (mysqli_connect_errno()) {
  printf("Falló la conexión: %s\n", mysqli_connect_error());
  exit();
}

The variable $mysqli will get the parameters or the data of the connection to the database.

Now the query of the records in the database:

   $consulta = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($resultado = $mysqli->query($consulta)) {

    /* obtener el array de objetos */
    while ($fila = $resultado->fetch_row()) {
    printf ("%s (%s)\n", $fila[0], $fila[1]);
    }

    /* liberar el conjunto de resultados */
    $resultado->close();
}

/* cerrar la conexión */
$mysqli->close();
?>

Reviewing this code I found an error at a glance.

if(mysqli_num_rows($registro)>0){
    while($registro2 = mysql_fetch_array($registro)){
        echo '';
    }
}

The error is here mysql_fetch_array you should check the code carefully to see these errors or some line or parameter or wrongly written variable.

  

Note: When you upgrade mysql to mysqli you must correctly change all the code to mysqli with no exceptions.

Instead of mysql_fetch_array replace with mysqli_fetch_array or% mysqli_fetch_assoc

I recommend you read this documentation

Greetings.

    
answered by 16.09.2016 в 03:43