getting data from MYSQL database to a html tabel using php - php

I design a table with html (bootstrap style) now i want to get data from my database table and fetch it in table using php, here is my html code:
Thanks for you help. :)
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
<th>Province</th>
<th>User</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
PHP code i tried but i didnt get any result it gives me errors:
<?php
$username = "root";
$password = "=";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
echo "<td>" . $row['Category'] . "</td>";
<td>{$row\['regdate'\]}</td>
<td>{$row\['occdate'\]}</td>
</tr>\n";
}
?>

Since mysql_* is depreciated I will answer this using mysqli_*.
You have already closed the table in your example before looping through the fetched data so I am unsure where you want these rows to populate however I made a guess.
Another error with your attempt is you have echo " ... echo "";
<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = 'SELECT * FROM table_one';
$data = mysqli_query($mysqli, $query);
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data))
{
echo " <tr>
<td>" . $row['Category'] . "</td>
<td>" . $row['regdate'] . "</td>
<td>" . $row['occdate'] . "</td>
</tr>";
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have indented the echo inside the while loop to keep it inline with the other html.

Related

Outputting Data into an HTML table using php/html

I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.

bootstrap display data from mysql in table

I am trying to display my mysql rows to html bootstrap table. Database connection is working, displaying data is working, but it is not fancy as i want. I'd like to maybe save in arrayData maybe and then print this arrayData in html tag. Please any suggestions much appreciated. I want to do this easiest way and most convenient for editing later on. php code to display data :
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["lastName"]. "<br>"; /* and so on..*/
}
} else {
echo "0 results";
}
and this is my html code for bootstrap
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>ales</td>
<td>king</td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">2</th>
<td>love</td>
<td>2</td>
<td>code</td>
<td></td>
</tr>
</tbody>
</div>
</table>
EDIT: i want this, but loaded from database not manually typed :)!
You can use a php loop this way
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php
....
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["id"]. '</td>
<td>' . $row["name"] .'</td>
<td> '.$row["lastName"] .'</td>
</tr>';
}
} else {
echo "0 results";
}
?>
</tbody>
</div>
</table>
I would do something like this:
( but you should really read some basic PHP )
<?php
echo "<table>";
// table header
echo "<tr><th>id</th><th>Name</th><th>Lastname</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["lastName"]."</td></tr>";
}
// table footer
echo "</table>";
?>

How can i echo sql attributes to an HTML table using PHP

<div class="col-md-6">
<?php
include 'includes/connection.php';
$query = "SELECT * FROM customer";
$result = mysql_query($query);
while ($person = mysql_fetch_array($result)) {
//echo "<p>" . $person['customerID'] . "</p>";
//echo "<p>" . $person['firstName'] . "</p>";
//echo "<p>" . $person['lastName'] . "</p>";
//echo "<p>" . $person['address'] . "</p>";
$customerID = $person['customerID'];
$firstName = $person['firstName'];
$lastName = $person['lastName'];
$address = $person['address'];
echo $customerID; //A test echo that is working just fine. But i need to echo this to the table
}
?>
<table class="table table-striped">
<tr>
<th>Customer ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
</th>
<tr>
<td>
<? echo $customerID; ?>
</td>
<td>
<? echo $firstName; ?>
</td>
<td>
<? echo $lastName; ?>
</td>
<td>
<? echo $address; ?>
</td>
</tr>
</table>
</div>
</div>
I am learning PHP and i need help with this as soon as possible.
When i run the web page nothing is being shown to the table apart from the table headers. Please help for i am learning PHP. I am using xampp on which i created a database.
<? echo $customerID; ?>
should be
<?php echo $customerID; ?>
Firstly:
Please, don't use mysql_* functions, They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi. This article will help you decide.
But to answer your question, you need to output your table data inside of your while loop like this:
<table class="table table-striped">
<tr>
<th>Customer ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
</th>
<?php while ($person = mysql_fetch_array($result)) { ?>
<tr>
<td>
<?php echo $person['customerID']; ?>
</td>
<td>
<?php echo $person['firstName']; ?>
</td>
<td>
<?php echo $person['lastName']; ?>
</td>
<td>
<?php echo $person['address']; ?>
</td>
</tr>
<?php } ?>
</table>

Why is my table being rendered empty?

I'm writing some php that renders a table from a database, this should be simple, but for some reason, it is rendering extra cells and all cells are empty. Here is my code:
<?php
$db= new PDO("mysql:host=localhost;dbname=mydb", "user", "password");
$query= $db->query("SELECT yarnId, yarnName, longYarnDescription, sale_price, cost, contents, onSale, yarnImage, activeFlag FROM yarn");
$result= $query->fetchAll();
?>
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php for($r=0; $r<count($result); $r++){?>
<tr>
<?php for($c=0; $c<count($result[0]); $c++){?>
<td><?php echo $result[r][c];?></td>
<?php }?>
<td><button name=edit>edit</button></td>
</tr>
<?php }?>
</table>
If anyone can tell me why it's empty and why there are extra cells, it would be greatly appreciated.
The following code uses while()loop instead of for()
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php
while($row = $query->fetch()) {
echo "<tr>";
for ($x=0;$x<= 8; $x++) {
echo "<td>" . $row[$x] . "</td>";
}
echo "<td><button name=\"edit\">edit</button></td></tr>\n";
}
?>
</table>

How to retrieve information from database and display in table?

This is my php code.
<?php
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid=$_POST['cnetid'];
$cpassword=$_POST['cpassword'];
$cnetid = stripslashes($cnetid);
$cpassword = stripslashes($cpassword);
$cnetid = mysql_real_escape_string($cnetid);
$cpassword = mysql_real_escape_string($cpassword);
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$result=mysql_query($sql) OR die(mysql_error());
$row=mysql_fetch_assoc($result);
$rentalid=$row['RentalID'];
$bikeid=$row['BikeID'];
?>
This is the html code. It should be displayihg the query that's been retrieved from the database but for some reason when I run it the table comes up blank. I know I'm only echoing two variables but even those come up empty.
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<table class="table table-striped">
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $rentalid ?></td>
<td><?php echo $bikeid ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table><br>
</div><!-- end span -->
Try this:
---------------------------EDITED-----------------------------------------
<?php
session_start();
if(isset($_POST['cnetid'])){
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid = mysql_real_escape_string($_POST['cnetid']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
$table = "<table class='table table-striped' >
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
";
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$body = "";
$result=mysql_query($sql) OR die(mysql_error());
while ($row=mysql_fetch_assoc($result))
{
$body = $body." <tr><td>".$row['RentalID']."</td>
<td>".$row['BikeID']."</td>
<td>".$row['RentalStartDate']."</td>
<td>".$row['RentalEndDate']."</td></tr> ";
}
$table = $table.$body."
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>" ;
}
else
{
$table = "No data..";
}
?>
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<?php echo $table; ?>
<br>
</div>
PS: Error found, corrected, and code tested xD
Saludos ;)

Categories