4214.11 View Data from MySQL Database

Step 1. You need a database connection.

$link = mysqli_connect ($host,$username,$password,$database);


Step 2. You need a SQL statement.

$query = "SELECT * FROM user";


Step 3. You need to open the connection and query the database.

$result = mysqli_query($link,$query);


Step 4. See if there is a result in the tables using an IF/ELSE statement.

if (mysqli_num_rows($result) > 0 ) {

//output each row

}

else {

echo "sorry nothing found";

}


Step 5. Output the result onto your page using a loop.

while ($row = mysqli_fetch_assoc($result)) {

echo "id: " . $row["id"].  " - Name: " .  $row["username"] . " Email: " . $row["email"]. "<br>";

}

Written by