PHP MySQL example

This example shows how to connect to a MySQL database and browse data using PHP:

<?php
$host= "localhost";
$username = "user";
$password = "pwd";
$dbname = "dbName";

// 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
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

 

You like this content ? Share it with you entourage !

Leave a Reply

Your email address will not be published.