Created
April 20, 2018 13:46
-
-
Save JuanjoSalvador/af54217b32cf09afc81a61fe38696c29 to your computer and use it in GitHub Desktop.
Example of MySQLi connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>MySQLi example</title> | |
<meta charset="utf8"> | |
</head> | |
<body> | |
<?php | |
// Usaremos una base de datos (foobar) con una tabla (users) que contiene username y email. | |
$user = "root" | |
$password = "mipasswordtopeguapens" | |
$ddbb = "foobar" | |
$con = new mysqli("localhost", $user, $password, $ddbb); | |
// select a lo bestia porque yolo | |
$sql_query= "select * from users"; | |
// amoh ahí palante esa consulta | |
$result = $con->query($sql_query); | |
// y ahora la sacamos por pantalla | |
echo "<h1>Lista de usuarios</h1>"; | |
echo "<table>"; | |
// la consulta devuelve un array asociativo, así que toca patearselo | |
while($row = $result->fetch_assoc()) { | |
echo "<tr><td>" . $row['username'] . "</td>" . "<td>" . $row['email'] . "</td></tr>"; | |
} | |
echo "</table>"; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment