Database Connection

Fetching Data From Database And Database Connection In PHP

Welcome to Exciting Tutorial On Fetch Data From Database And Database Connection In PHP.
In the example above, the data returned by the mysqli_query() function is stored in the $result variable. Each time mysqli_fetch_array() is invoked, it returns the next record from the result set as an array. The while loop is used to loops through all the records in the result set. Finally the value of individual fields can be accessed from the record either through passing the field index or the field name to the $row variable.





CODE

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‘root’ with no password) */
$link = mysqli_connect(“localhost”, “root”, “”, “ecommerce”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. ” . mysqli_connect_error());
}

// Attempt select query execution
$sql = “SELECT * FROM brands”;
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo “<table>”;
echo “<tr>”;
echo “<th>brand_id</th>”;
echo “<th>brand_title</th>”;

echo “</tr>”;
while($row = mysqli_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘brand_id’] . “</td>”;
echo “<td>” . $row[‘brand_title’] . “</td>”;

}
echo “</table>”;
// Close result set
mysqli_free_result($result);
} else
{
echo “No records matching your query were found.”;
}
} else{
echo “ERROR: Could not able to execute $sql. ” . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

Post Author: educlash

Leave a Reply