How to connect and display data from a database in PHP

0

I want to connect a database to a PHP page, in another that I have it works well, but it is this I need to connect and show inside the echo. I have this that I use in another page:

<?php 

	$conexion=mysqli_connect('localhost','user','mypass','mibase');

 ?>
 
 //Esto lo pongo arriba de la etiqueta <html>
 
<?php 
		$sql="SELECT * from dpartidos";
		$result=mysqli_query($conexion,$sql);

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

// Y para mostralo lo hago así

<li><?php echo $mostrar['dia1'] ?></li>

//Pero necesito ponerlo acá:

//...PHP

if ($quediaes=="Tue" && 11 <= $hora && $hora <= 12 ) {

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

	while($mostrar=mysqli_fetch_array($result)){
echo '<center>
<h3>Ahora EN VIVO: 
  <a href="#" 
     onclick="window.open( \'https://paginas.com\', \'_self\');
              window.open( \'https://paginas.com\', \'_blank\');" >' . $mostrar['autor'] . '</a>     
</h3>
</center>';

else if ($quediaes=="Tue" && 15 <= $hora && $hora <= 16 ) {
echo '<center>
<h3>Ahora EN VIVO: 
  <a href="#" 
     onclick="window.open( \'https://www.\', \'_self\');
              window.open( \'https://www.\', \'_blank\');" >
  UN PARTIDO - 
  </a>
  <a href="#" 
     onclick="window.open( \'https://www.\', \'_self\');
              window.open( \'https://www.\', \'_blank\');" >
  UN PARTIDO
  </a>      
</h3>
</center>';
}
}
echo '</div>';

?>
    
asked by MatiPHP 27.06.2018 в 01:42
source

1 answer

3

You have to learn to concatenate or use double quotes.

For the concatenate in PHP the point is used, example:

$variable = 'una variable';
echo 'texto concatenado con ' . $variable . ' en un echo';
// resultado
// texto concatenado con una variable en un echo

Also in php when we use double quotes the php variables are expanded, example:

$variable = 'una variable';
echo "texto concatenado con $variable en un echo";
// resultado
// texto concatenado con una variable en un echo

Here you can check the character string documentation .

Therefore your code could look something like this:

echo '<center>
    <h3>Ahora EN VIVO: 
      <a href="#" 
         onclick="window.open( \'https://paginas.com\', \'_self\');
                  window.open( \'https://paginas.com\', \'_blank\');" >' . $mostrar['dia1'] . '</a>     
    </h3>
    </center>';
    
answered by 27.06.2018 / 02:02
source