Skip to content

Instantly share code, notes, and snippets.

@JuanjoSalvador
Created April 20, 2018 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JuanjoSalvador/af54217b32cf09afc81a61fe38696c29 to your computer and use it in GitHub Desktop.
Save JuanjoSalvador/af54217b32cf09afc81a61fe38696c29 to your computer and use it in GitHub Desktop.
Example of MySQLi connection
<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