Fetching data
Once the connection with a MySQL database is successful, the next logical step is to fetch the data from this database table. In this section, we will see how we would fetch the data from a database table once we are connected to it.
Let us continue editing the db.php
file. We will add the following code in the else block where we were just displaying a successful message. Displaying a success message is not very meaningful, and so we will replace that echo
statement with the following code:
$sql = "SELECT Event_ID, Event_Name FROM Events_Catalog"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "Event ID is: <b>" . $row["Event_ID"] . "</b><br /> Event name is: <b>" . $row["Event_Name"] . "</b><br /><br />"; } } else { echo "<h3>Sorry, currently there are no scheduled events.</h3>"; }
Again, as you...