Skip to content

Instantly share code, notes, and snippets.

@chanwit
Created January 6, 2017 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanwit/f24772f40d5c1f392b15aa139626b716 to your computer and use it in GitHub Desktop.
Save chanwit/f24772f40d5c1f392b15aa139626b716 to your computer and use it in GitHub Desktop.
<?php
$servername = "mysql";
$username = "root";
$password = "1234";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment