how can I show a table with php and mysql

0

I want to show a table but at the moment of showing it I get entangled I need help to show it and I can connect but not import it

this is the code to connect me:

<?php
	function conectarse (){
		$link = mysqli_connect("localhost", "root", "*********") or die("No se pudo conectar: " . mysql_error());
		if (!$link) {
			echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
			echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
			echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
			exit;
		}
		
		
		if(!mysqli_select_db($link,"precioproductos")){
			echo "Error: No se pudo conectar con la base de datos." . PHP_EOL;
			exit;
		}
		return $link; 

	}
	
		

?>
    
asked by chris 01.04.2018 в 23:35
source

1 answer

2

Create a .php file and paste the following code, replace it with the values of your connection and the data that your query returns.

<html>

<body>
    <?php/* Abrimos la base de datos */
  	$conx = mysql_connect ("localhost","root","");
  	if (!$conx) die ("Error al abrir la base <br/>". mysql_error()); 
  	mysql_select_db("test") OR die("Connection Error to Database");    

  	/* Realizamos la consulta SQL */
  	$sql="select * from usuario";
  	$result= mysql_query($sql) or die(mysql_error());
  	if(mysql_num_rows($result)==0) die("No hay registros para mostrar");

  	/* Desplegamos cada uno de los registros dentro de una tabla */  
	echo "<table border=1 cellpadding=4 cellspacing=0>";

	/*Primero los encabezados*/
 	echo "<tr>
         	<th colspan=5> Agenda personal </th>
       	<tr>
         <th> ID </th><th> Nombre </th><th> Apellido </th> <th> Teléfono </th><th> Email </th>
      </tr>";


      /*Y ahora todos los registros */

      while($row=mysql_fetch_array($result)){
 		echo "<tr>
         <td align='right'> $row[id] </td>
         <td> $row[nombre] </td>
         <td> $row[apellido] </td>
         <td> $row[telefono] </td>
         <td> $row[email] </td>
      </tr>";}

      echo "</table>";
	?>
</body>

</html>
    
answered by 02.04.2018 в 00:55