Go through a MySQL table in PHP


I have the following table, called test, in MySQL :

Now I would like to rescue this data in PHP , but I can not clarify my idea about how is the most correct way to do it. That is, I'm not supposed to know how many rows or fields the table has, but depending on what I have obtained, I can paint a table in PHP with that data.

How would it be the right way to do it?

asked by lk2_89 21.11.2018 в 10:03

2 answers

$server = "localhost";
$user = "usuario";
$password = "contrasenna";
$bd = "base_datos";

$conexion = mysqli_connect($server, $user, $password, $bd);

if (!$conexion){ 
    die('Error de Conexión: ' . mysqli_connect_errno());    

mysqli_set_charset($con, 'utf8');

$query = "SELECT * FROM tabla_base_datos";
$result = mysqli_query($conexion, $query);

if (mysqli_num_rows($result) > 0) {
    while($fila = mysqli_fetch_assoc($result)){
        echo $fila["campo1_tabla"];
        echo $fila["campo2_tabla"];
        echo $fila["campo3_tabla"];
} else {
    die("Error: No hay datos en la tabla seleccionada");

answered by 21.11.2018 / 13:32


$db = new mysqli('host','database_user','database_pass','database_name');
   echo $db->connect_error;
$result = $db->real_query("Select * From tabla'");

In $ db you configure the connection. With the if you check connection errors And in the variable $ result you store the result of executing the query. (The table and the configuration data you have to adapt to what you have configured)

Adapted from: link

answered by 21.11.2018 в 11:50