How To Display Data From A Database
Solution 1:
You can use the fetch_row function in combination with a while loop. This is the easiest way. Read more about the function here: http://www.php.net/manual/en/mysqli-result.fetch-row.php
For example:
if ($result = mysqli_query($link, $query)) {
/* fetch associative array and print result*/while ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)\n", $row[0], $row[1]);
}
Solution 2:
If I understand your question correctly, you and asking how to output data from the database directly to the screen. Without offering any formatting, this might be what you are looking for.
$sql="SELECT * FROM $tbl_name WHERE username='$myusername'";
$result=mysql_query($sql);
$results_array=mysql_fetch_array($result);
Once you have the $results_array, you can access any fetched columns from the database using normal keyed array syntax, such as:
echo$results_array["username"];
Hopefully that helps.
Solution 3:
As an example:
$result = mysql_query($sql);
if(! $result) die("Error executing query");
while($row = mysql_fetch_assoc($result) {
echo$row["name"];
echo$row["tracking"];
echo$row["status"];
}
Or, you can do it the object oriented way:
$result = mysql_query($sql);
if(! $result) die("Error executing query");
while($row = mysql_fetch_object($result) {
echo$row->name;
echo$row->tracking;
echo$row->status;
}
On a related note, if the only fields you want are name, tracking, and status, you should only select those fields in your query. It will make the query faster, as well as prevent data from being fetched that you don't want (which can be more secure)
"SELECT name, tracking, status FROM $tbl_name WHERE username='$myusername'";
Solution 4:
A traditional way from start to finish could be as follows.
<?php$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connectionif (mysqli_connect_errno())
{
echo"Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
while($row = mysqli_fetch_array($result))
{
echo$row['FirstName'] . " " . $row['LastName']; //**Put all your formatting in here!**echo"<br />";
}
mysqli_close($con);
?>
Post a Comment for "How To Display Data From A Database"