How to extract a database query (MYSQL) to a PHP page

-1

I would like to know how I can extract the information collected in a php query variable, to see it in my html document to query the database.

ANNEX: PHP-HTML CODE:

<!DOCTYPE html>  
<html>  
<head>   <title>CRITICO</title>  
</head>  
<body>  
    <?php     
        $con=mysqli_connect('127.0.0.1','root','','freatico') or die ('Error en la conexion');  
        $sql="SELECT * FROM medicion WHERE Nivel>=1.20";  
        $resultado=mysqli_query($con,$sql) or die ('Error en el query database');  
    ?>  
</body>  
</html> 
    
asked by Ângel Dâvíd Bermëo 21.05.2018 в 01:06
source

2 answers

2
  

I'll give you a complete code so you can learn control of your code,   if you only want to extract the data from the query, with the WHILE   enough

<?php 
$con=mysqli_connect('127.0.0.1','root','','freatico') or die ('Error en la conexion');  
    $sql="SELECT * FROM medicion WHERE Nivel>=1.20";  
    $resultado=mysqli_query($con,$sql) or die ('Error en el query database');
//Valida que la consulta esté bien hecha
if( $resultado ){

  //Ahora valida que la consuta haya traido registros
  if( mysqli_num_rows( $resultado ) > 0){

    //Mientras mysqli_fetch_array traiga algo, lo agregamos a una variable temporal
    while($fila = mysqli_fetch_array( $resultado ) ){

      //Ahora $fila tiene la primera fila de la consulta, pongamos que tienes
      //un campo en tu DB llamado NOMBRE, así accederías
      echo $fila['NOMBRE'];
    }

  }

  //Recuerda liberar la memoria del resultado, 
  mysqli_free_result( $resultado );

  //Si ya no ocupas la conexión, cierrala
  mysqli_close( $con );

}


 ?>
    
answered by 21.05.2018 / 01:21
source
0

You solve it in several ways but I'll show it to you with a WHILE loop

  

As I show you in my example the HTML tags have been added and   already with css you can apply the style you want

Since you do not show the structure, that is, the columns of your table, you can add an example

<!DOCTYPE html>  
<html>  
<head>   <title>CRITICO</title>  
</head>  
<body>  
    <?php     
        $con=mysqli_connect('127.0.0.1','root','password','blog') or die ('Error en la conexion');  
        $sql="SELECT * FROM posts";  
        $resultado=mysqli_query($con,$sql) or die ('Error en el query database');  
        while($fila = $resultado->fetch_assoc())
        {
            echo ("<p>".$fila["title"]."</p>");
            echo ("<p>".$fila["body"]."</p>");
        }

    ?>  
</body>  
</html> 

As you can see I do a while, since as a loop it helps me to navigate the entire dataset I am receiving from the server query.

The variable $fila is the one that stores the result of this query, therefore inside brackets I will pass the name of the key or column of my table to which I want to access.

I use fetch_assoc to be able to treat the dataset I am receiving as an associative array

  

Now at the end to add HTML tags, I just put them between   quotes and concatenated or pasted my variable row at the beginning and at the   final; in this case I used a p-label and with the sign of. he   concatena

UPDATE

If you want to show it in a table just take a look at the example that I leave below; the styles I leave to you so that you put them

<!DOCTYPE html>  
<html>  
<head>   <title>CRITICO</title>  
</head>  
<body>  
    <table>
        <tr>
            <th>title</th>
            <th>body</th>
        </tr>
        <tr>
            <?php     
            $con=mysqli_connect('127.0.0.1','root','password','blog') or die ('Error en la conexion');  
            $sql="SELECT * FROM posts";  
            $resultado=mysqli_query($con,$sql) or die ('Error en el query database');  
            while($fila = $resultado->fetch_assoc())
            {
                echo ("<td>".$fila["title"]."</td>");
                echo ("<td>".$fila["body"]."</td>");
            }

            ?>  
        </tr>
    </table>

</body>  
</html> 
    
answered by 21.05.2018 в 01:19