Does not show the data of my MySQL table

1

I want to show in my php file taking from my MySQL database the ClientCode and CustomerName of my table Customers of my Database 'gardening', this is the code I have but nothing is shown to me in the browser. The connection to the database works correctly since I do not get the error message .. Where is the error?

<?php

$conex=mysqli_connect("localhost","root","","jardineria");

if($conex->connect_errno){

  echo "ERROR EN LA CONEXION BDD";
  exit();

}
else{



  $consulta="SELECT*from Clientes";
  $conex=mysqli_query($conex, $consulta);



  while($fila=mysqli_fetch_array($conex, MYSQL_ASSOC)){

      echo $fila['CodigoCliente'];
      echo $fila['NombreCliente'];
  }

  }

?>
    
asked by davescode 04.12.2018 в 23:21
source

3 answers

1

Actually your code is missing several controls:

  • If the query fails
  • If you do not throw rows

I have added those two controls, and also one to the connection, although you say it works. Anyway this is a strictly controlled code that collects in a variable $msg anything that happens, finally showing on the screen the result.

I have commented on the same code those things that might be new to you. I've used the object-oriented style in everything, it's more modern, more elegant, lighter, more intuitive. Everything negative is at the end, in else . I do not think that the bad thing should never be put before:)

I hope you find it useful:

<?php 
    /*Controlamos la validez de la conexión*/   
    if ( $conex = new mysqli("localhost","root","","jardineria") ){
        $sql="SELECT * FROM Clientes";
        /*Controlamos un posible fallo de la consulta*/
        if ( $result = $conex->query($sql) ) {
            /*Verificamos  cuántas filas trajo la consulta*/
            $totalFilas = $result->num_rows;    
            if($totalFilas>0){
                $msg=printf("Se encontraron %d filas.\n", $totalFilas);
                /*Usamos un método más específico*/
                while($fila = $result->fetch_assoc()){
                    $msg.=$fila['CodigoCliente']." - ".$fila['NombreCliente'].PHP_EOL;
                }
            } else {
                $msg="No se encontraron registros";
            }
        } else {
            $msg=printf("Error en la consulta %s", $conex->error);
        }
    } else { 
        $msg="Error de conexión";
    }
    /*Imprimimos lo que recogió $msg*/
    echo $msg;
?>
    
answered by 14.12.2018 в 04:55
0

I have not tried it, but according to the examples that I have done, this is how they worked for me (inside the if this is a function)

< ?php
$conex=mysqli_connect("localhost","root","","jardineria");
if($conex->connect_errno){
  echo "ERROR EN LA CONEXION BDD";
  exit();
}
else{
  $consulta="SELECT*from Clientes";
  $results=mysqli_query($conex, $consulta);

  while($fila=mysqli_fetch_array($results)){
      echo '<tr><td>' .$fila['CodigoCliente'].'< /td>< 
td>'.$fila['NombreCliente'].'< /td>< /tr>;
  }
  }
? >
    
answered by 04.12.2018 в 23:49
0

I told you that you could handle your code in the following way

$conex= new mysqli("localhost","root","","jardineria");

if($conex->connect_errno){
    return $conex->connect_error;
}
else{
    echo "Conectado";
}


$consulta= $conex->query("SELECT * FROM Clientes");

foreach($consulta as $dato)
{
    echo $dato["CodigoCliente"];
    echo $dato["NombreCliente"];
}

EXPLANATION OF THE CODE

  • I declare the connection with new mysqli
  • To get the error that occurred if it does not connect, I do $conex->connect_error;
  • To execute the query, since you do not receive dynamic values from the user, it is in this way $conex->connect_error;
  • with a loop foreach I do the route of the dataset that is being returned to me and with the variable $dato I access the name of each column to print it
  • To be able to execute the query, it would be better to be like this: $consulta = $conexion->query("aqui el query.........");
  • answered by 04.12.2018 в 23:49