Error Mysqli fetch array PHP MYSQL [duplicate]

0

I'm trying to connect with php mysql , they gave me an old code, use mysql classic then I wanted to use mysqli but I get an error in

  

mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given "

I saw other pictures but it does not fit anything, I would like to know why I get an error

function consultaUsuarios($conexion){
$conex= new mysqli('localhost','root','root','call_center');
$salida='';
//Realizamos la Consulta que nos traera todos los registros de la BD
$query="select id,marca,producto,nombre,area
 from creacion";
$total=mysqli_query($conex,$query);
$consulta=$conex->query($query);
 //Validamos si hay o no registros
 if(!$total || mysqli_num_rows($total) == 0){
     while($dato=mysqli_fetch_array($total)){
         $salida.='
            <tr>
                <td>'.$dato["id"].'</td>
                <td>'.$dato["marca"].'</td>
                <td>'.$dato["producto"].'</td>
                <td>'.$dato["nombre"].'</td>
                <td>'.$dato["area"].'</td>
                <td class="'.returnStatus($dato["status"]).'">'.$dato["status"].'</td>
                <td ><a class="btn btn-small">Editar</a></td>
            </tr>
         ';
     }
 }
 else
 {
     $salida='
        <tr id="sinDatos">
            <td colspan="7">No hay Registros en la Base de Datos, Tu codigo!!</td>
        </tr>
     ';
 }

The connection I put in just to practice something fast

    
asked by Esteban Tolvar 07.06.2017 в 18:34
source

2 answers

0

It has inconsistencies in your code. Where do you use $ query? You are making a query that you are not using. Second. Validation logic if the query brought data or is not wrong.

function consultaUsuarios(){//ELIMINA EL PARAMETRO DE CONEXIÓN POR QUE NO LA ESTÁS USANDO, YA TIENES UNA DENTRO
$conex= mysqli_connect('localhost','root','root','call_center'); //CORREGI TU CONEXIÓN A MYSQLI
$salida='';
//Realizamos la Consulta que nos traera todos los registros de la BD
$query="select id,marca,producto,nombre,area
 from creacion";
$total=mysqli_query($conex,$query);
//QUITAMOS LA CONSULTA DE MYSQL Y NOS QUEDAMOS CON MYSQLI
 //Validamos si hay o no registros
 if($total && mysqli_num_rows($total) < 0){//LA LOGICA ESTA MAL, AHÍ ESTÁ CORREGIDA
     while($dato=mysqli_fetch_array($total)){
         $salida.='
            <tr>
                <td>'.$dato["id"].'</td>
                <td>'.$dato["marca"].'</td>
                <td>'.$dato["producto"].'</td>
                <td>'.$dato["nombre"].'</td>
                <td>'.$dato["area"].'</td>
                <td class="'.returnStatus($dato["status"]).'">'.$dato["status"].'</td>
                <td ><a class="btn btn-small">Editar</a></td>
            </tr>
         ';
     }
 }
 else
 {//SE TE BARRIÓ PONER EL PUNTO PARA CONCATENAR LA SALIDA
     $salida.='
        <tr id="sinDatos">
            <td colspan="7">No hay Registros en la Base de Datos, Tu codigo!!</td>
        </tr>
     ';
 }
echo $salida; //MUESTRALA
    
answered by 07.06.2017 в 19:04
0

You are duplicating the query:

//$total=mysqli_query($conex,$query); consulta 1

$consulta=$conex->query($query);     nuevamente consulta

then your validation is erroneously expressed:

i f(!$total || mysqli_num_rows($total) == 0)

you indicate that your query has to be false and it should not have records your query, you must change your condition.

if(mysqli_num_rows($consulta) > 0)

then wanting to visit your query is also wrongly expressed:

while($dato=mysqli_fetch_array($total))

By doing that I will never go through the values correct me if I'm wrong.

I would recommend you do the following:

    $array = mysqli_fetch_all($consulta); // almacenar todos tus registros dentro de un variable de tipo array

        foreach ($array as $item) // recorrer registro por registro
        {
           // imprimir los valores aqui 
            echo '<tr>';// lo que hago a partir de aqui es opcional, solo si quieres personalizar tus columnas
            foreach ($item as $do)//recorres las columnas del registro
            {
                echo '<td>'.$do.'</td>';//imprimes la columna
            }
            echo '<tr>';
        }
    
answered by 07.06.2017 в 19:40