Display data from two mysql tables in php

1

I want to show data from two tables from the same database on the same PHP page.

One table is called "departures" and the other is called "dpartidos2".

Now, I want to show a data that is called "match3" that is in "split2"

My code:

$sql="SELECT * from dpartidos ";
$result=mysqli_query($conexion,$sql);

while($mostrar=mysqli_fetch_array($result)){

if ($quediaes=="Wed" && 0 <= $hora && $hora <= 10) {
echo '<h3>No hay ningun partido en este momento</h3>';
}
else if ($quediaes=="Wed" && 11 <= $hora && $hora <= 12) {
  echo '<h3>Estás mirando: ' . $mostrar['partido1'] . '</h3>';
}

else if ($quediaes=="Wed" && 13 <= $hora && $hora <= 14) {
  echo '<h3>A continuacion: ' . $mostrar['partido3'] . '</h3>';
}
else if ($quediaes=="Wed" && 15 <= $hora && $hora <= 17 ) {
  echo '<h3>Estás mirando: ' . $mostrar['partido3'] . '</h3>';
}
else if ($quediaes=="Wed" && 16 <= $hora && $hora <= 23) {
echo '<h3>No hay ningun partido en este momento</h3>';
}
}
    
asked by MatiPHP 27.06.2018 в 23:06
source

2 answers

2

If you need data from different tables you can bring the data of both in different variables by means of a separate query for each one.

// Este es tu primer set de datos que trae dpartidos
$sql_dbpartidos = "SELECT * from dpartidos";
$result_dbpartidos = mysqli_query($conexion,$sql_dbpartidos);
$rows_dbpartidos = mysqli_fetch_array($result_dbpartidos);

// Este es tu segundo set de datos que trae dpartidos2
$sql_dbpartidos2 = "SELECT * from dpartidos2";
$result_dbpartidos2 = mysqli_query($conexion,$sql_dbpartidos2);
$rows_dbpartidos2 = mysqli_fetch_array($result_dbpartidos2);

if($rows_dbpartidos){

    if ($quediaes=="Wed" && 0 <= $hora && $hora <= 10) {
        echo '<h3>No hay ningun partido en este momento</h3>';
    }
    else if ($quediaes=="Wed" && 11 <= $hora && $hora <= 12) {
        echo '<h3>Estás mirando: ' . $rows_dbpartidos['partido1'] . '</h3>';
    }
    else if ($quediaes=="Wed" && 13 <= $hora && $hora <= 14) {
        if($rows_dbpartidos2){
            echo '<h3>A continuacion: ' . $rows_dbpartidos2['partido3'] . '</h3>';
        }
    }
    else if ($quediaes=="Wed" && 15 <= $hora && $hora <= 17 ) {
        if($rows_dbpartidos2){
            echo '<h3>Estás mirando: ' . $rows_dbpartidos2['partido3'] . '</h3>';
        }
    }
    else if ($quediaes=="Wed" && 16 <= $hora && $hora <= 23) {
        echo '<h3>No hay ningun partido en este momento</h3>';
    }

}

Or in any case you can do a join which in a single query allows you to join the data of different related tables.

    
answered by 28.06.2018 / 04:09
source
1

You must make another query based on data, since the one you have only brings you the data of departures. You should do the following:

 $sql2="SELECT * from dpartidos2";
$result2=mysqli_query($conexion,$sql);

and when you want to call the second table you use that variable result2.

    
answered by 28.06.2018 в 00:54