Problem with select without where to a database from php

1

<?php
$mysqli = new mysqli('localhost','root','alumno','radius');
if (mysqli_connect_errno()){
echo "Fallo al conectar a MySQL:";
exit();
}

$query="SELECT id,nombre,autor FROM canciones";
$stmt=$mysqli->prepare($query);
$stmt->bind_param("iss",$id,$nombre,$autor);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0)
{
	$stmt->bind_result($id,$nombre,$autor);
?>

<html>
<body>
	<h1>LISTADO DE CANCIONES DISPONIBLE</h1><br />
	 <table>	 
	 <tr> <th><b>Canciones Disponibles</th><th><b>ID</th><th><b>NOMBRE</th><th><b>AUTOR</th>
</tr>
<?php

while ($stmt->fetch()){
?>	

<tr> 
	<td> <?php echo strtoupper($id);?></td>
	<td> <?php echo strtoupper($nombre);?></td>
	<td> <?php echo strtoupper($autor);?></td>
</tr> 
	
<?php
}
?>
</table>
 <br>
<table>
 <tr><td><a href="peticiones.php"><button  btn" >Pedir una cancion...</button></a></td></tr>
 </table><br>
  </body>
 </html>   
<?php
}
else
{
?>

<?php
$mysqli->close();
?>	</body>
</html>

I'm trying to select a database but the page goes blank. I do not know if he's doing something

I attach the code.

    
asked by Saúl Villarín 07.06.2016 в 15:57
source

2 answers

0

There are several problems in the code above. The one that creates the problem you see (with the page blank) is that there is a else { that does not close and therefore you get an "Unexpected end of the document" error and the code is not processed.

The solution is simple: close the key of else and everything will be executed.

<?php
}
else
{
?>

<?php
}
$mysqli->close();
?>  </body>
</html>

Now you will see content on the page, but there will still be other errors that I do not know if they occur because you have reduced the code you share in the question: variables that are not defined are used ( $id , $nombre and $autor ); and a bind_param is made but in the SQL statement there are no parameters to do a bind.

    
answered by 07.06.2016 / 16:27
source
0

It is possible that the error is in this block.

  $mysqli = new mysqli('localhost','root','alumno','radius');
if (mysqli_connect_errno()){
echo "Fallo al conectar a MySQL:";
exit();
}

When you give an error in the connection the exit (), would prevent the execution of the program, hence the page will go blank. Check the data of the database. You can also put a string to the exit () to see if it is giving the error there, as follows:

exit("Error en la conexión");
    
answered by 07.06.2016 в 16:25