How to show the data of a database in a table in html and php

2

I'm new to php and html and I want to show the data in a custom table by bootstrap, this is my code

<?php

// Conectando, seleccionando la base de datos

$link = mysql_connect('localhost', 'root', 'usbw')

    or die('No se pudo conectar: ' . mysql_error());


mysql_select_db('hotel') or die('No se pudo seleccionar la base de datos');



// Realizar una consulta MySQL
$query = 'SELECT id, nombre, apellido, telefono, dias, habitaciones FROM registro';

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

?>

<table class="table table-striped">
  	
		<thead>
		<tr>
			<th>ID</th>
			<th>NOMBRE</th>
			<th>APELLIDO</th>
			<th>TELEFONO</th>
			<th>ESTADIA</th>
			<th>HABITACION</th>
		</tr>
		</thead>
<?php while ($row = mysql_fetch_array($result)){?>
	<td><?php $row['id'] ?></td>
    <td><?php $row['nombre'] ?></td>
    <td><?php $row['apellido'] ?></td>
    <td><?php $row['telefono'] ?></td>
    <td><?php $row['dias'] ?></td>
    <td><?php $row['habitaciones'] ?></td>
	
<?php} ?>
</table>
    
asked by Elias Quintanilla 14.06.2017 в 02:57
source

1 answer

2

Remember that the html format. for your document of agreement with what you want to do with bootstrap.

I recommend you read the php documentation link , and also try to separate the php code from html for good practices.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<!-- jQuery library -->
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

	<!-- Latest compiled JavaScript -->
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<?php
// Te recomiendo utilizar esta conección, la que utilizas ya no es la recomendada. 
$link = new PDO('mysql:host=localhost;dbname=hotel', 'root', ''); // el campo vaciío es para la password. 

?>

<table class="table table-striped">
  	
		<thead>
		<tr>
			<th>ID</th>
			<th>NOMBRE</th>
			<th>APELLIDO</th>
			
		</tr>
		</thead>
<?php foreach ($link->query('SELECT * from registros') as $row){ // aca puedes hacer la consulta e iterarla con each. ?> 
<tr>
	<td><?php echo $row['id'] // aca te faltaba poner los echo para que se muestre el valor de la variable.  ?></td>
    <td><?php echo $row['nombre'] ?></td>
    <td><?php echo $row['apellido'] ?></td>
 </tr>
<?php
	}
?>
</table>
</body>
</html>

I hope to be of help.

Greetings !!!

    
answered by 14.06.2017 в 04:31