Está en la página 1de 4

I.E.S.T.

P ”ALEXANDER VON HUMBOLDT”

UNIDAD DIDÁCTICA

TALLER DE PROGRAMACIÓN WEB


TEMA:

VISUALIZAR REGISTROS DE UN
FORMULARIO

DOCENTE

Ing. WILBER ACUÑA SOLANO


Recuperar datos de MySQL usando
PHP
Para recuperar datos de MySQL se usa la instrucción SELECT. Podemos
recuperar datos de una columna específica o de todas las columnas de una
tabla.

Para recuperar los datos de la columna seleccionada de la base de datos, la


consulta SQL es

SELECT column_name, column_name FROM table_name;

Para recuperar todos los datos de columna de una tabla, la consulta SQL es

SELECT * FROM table_name;

En el siguiente ejemplo, recuperamos los datos de la base de datos MySQL.

En este ejemplo usamos 2 archivos para recuperar datos

• database.php- Para conectar la base de datos.


• retrieve.php: para recuperar datos de la base de datos

database.php
<?php
$url='localhost';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"crud");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
?>

retrieve.php
<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM employee");
?>
<!DOCTYPE html>
<html>
<head>
<title> Recuperar datos</title>
</head>
<body>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<table>

<tr>
<td>Nombre</td>
<td>Apellido</td>
<td>Ciudad</td>
<td>Correo</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["city_name"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</body>
</html>

También podría gustarte