getting data from database to html table using php - php

i want to fetch data from database and insert it in my html table here is my code i dont know where is my misatake:
<div class="ibox-content">
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, lastname, phone FROM regis";
$result = $conn->query($sql);
$conn->close();
?>
<table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Acc Type</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>";
<td>0795934799</td>
<td class="center">demo#demo.com</td>
<td>Admin</td>
}
echo "</table>";
}
?>
</tbody>
</table>
</div>
data i fetch from database shows out of table.
sorry for bad English :)

Theres just a Syntax error in your while loop. You need to print everything inside the echo command. As you can see in yours, you have echo(...); followed by some HTML still inside the php. So you should correct it by changing it to the following.
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td></tr>
<td>0795934799</td>
<td class=\"center\">demo#demo.com</td>
<td>Admin</td>";
}
A good resource that you can use is phpchecker.com that checks your code for errors

Here is the code that works...
<?php
$servername = "localhost";
$username = "sehnoqta_userbmc";
$password = "u?gQ=uS%t;a?";
$dbname = "sehnoqta_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, lastname, phone FROM regis";
$result = $conn->query($sql);
$conn->close();
?>
<table dir="rtl" class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Acc Type</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td>";
"<td>0795934799</td>";
"<td class='center'>demo#demo.com</td>";
"<td>Admin</td></tr>";
}
echo "</table>";
}
Hope this helps......

echo
"<tr><td>" . $row["name"]. "</td>
<td>" . $row["lastname"]. "</td>
<td>" . $row["phone"]. "</td>
<td>0795934799</td>
<td class='center'>demo#demo.com</td>
<td>Admin</td></tr>";

Related

Merge and display 2 database and 2 tables

Hey guys i have a problem, there is a page where i need to display data acording to date, but from 2 databases and from 2 tables from each databases,
1 Database name = highmob_comenzi Tables = players and vanzari
2 Database name = highmob_comenzi2 Tables = players and vanzari
this is the code what i got , i have tryed "select * from players, vanzari" but still no luck, even that wont extract data from both databases :(
<table class='table table-responsive-sm table-bordered table-striped table- sm' border="2px">
<thead>
<tr>
<th>Locatia Vanzari</th>
<th>Tip. Cert.</th>
<th>Nr.</th>
<th>Status Comanda Mobila</th>
<th>Status Comanda Tapiterii</th>
<th>Status Livrare</th>
<th>Ora Livrare</th>
<th>Detalii Comanda</th>
<th>Total</th>
<th>Avans</th>
<th>Rest</th>
<th></th>
</tr>
<?php
$servername = "localhost";
$username = "id";
$password = "pw";
$dbname = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
WHERE statuslivrare >= CURRENT_DATE()");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
</tbody>
</table>
you can access to the table in the same query without problem eg (assuming the tables have the same struct in both the database):
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
this is only a sample for accessing to the 2 database in a single query. you must rethink the query with you join and condition
but based on you comment seem you need just (assuming that statuslivrare is a colum of the vanzari table)
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
WHERE vanzari.statuslivrare >= CURDATE()");
<?php
$servername = "localhost";
$username = "ID";
$password = "PW";
$dbname1 = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname1 );
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select * from highmob_comenzi2.players cross join highmob_comenzi2.vanzari
");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
This is what i got and its working, it displays data from 1 database nammed highmob_comenzi2 and tables players and vanzari, but im getting alot of duplicates on display and also i need to insert a WHERE clause :
WHERE statuslivrare >= CURRENT_DATE()
any ideea?

PHP - Display Serial Numbers automatically using for loop

<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<?php
$result = mysqli_query($connect,"SELECT * FROM movies ;")or die(mysqli_error());
$rows = mysqli_fetch_array($result);
?>
<tbody>
<?php
$counter=0;
foreach($result as $rows)
{
$counter=$counter+1;
?>
<?php echo "<tr><td>" . $counter . "</td><td>" . $rows['language'] . "</td><td>" . $rows['movie_name'] . "</td></tr>"; ?>
<?php } ?>
</tbody>
I want to display serial numbers in the table automatically for the fetched results . Here the serial number displays correctly for the first five results like 1,2,3,4,5 rows
but on the 2nd page the number shows like 8,9,10,6,7
Where i am making mistake ? I even tried while loop and forloop increment counter. Im using Bootstrap data table to display the results from database.
<?php
$result = mysqli_query($connect,"SELECT * FROM movies");
$counter=0;
while($rows = mysqli_fetch_array($result)){
echo "<tr><td>" . $counter . "</td>
<td>" . $result[$counter]['language'] . "</td>
<td>" . $result[$counter]['movie_name'] . "</td>
</tr>";
$counter++;
}
?>
This should work for you. You need to fetch the results inside a loop in order to parse them.
Try this
You have to declare the $i=1 outside of the while loop and $i++ inside the while loop. So it will display the number of rows available in the database.
You have to use while loop instated of foreach.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM movies";
$result = mysqli_query($conn, $sql);
//mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<tbody>
<?php
$i=1;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($rows = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $i . "</td>
<td>" . $rows['language'] . "</td>
<td>" . $rows['movie_name'] . "</td>
</tr>";
$i++;
}
} else {
echo "0 results";
}
?>
</tbody>
</table>
</body>
</html>

PHP if statement in html-table outputs error

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.
You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";
Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)
This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";
Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query

Displaying results from mysql database in a html/php table

Hi I've got a page where I can view data output from a mysql database. It works good. But it's not stylish. So I decided to put in a html <table> tag. However it's not displaying the data.
I've tried putting in
<td> </td> etc between rows but it stops the code being parsed.
<?php
$servername = "******";
$username = "root";
$password = "********";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT first_name, last_name, email FROM person_list";
$result = $conn->query($sql);
//display table
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<br> ". $row["first_name"]. " "
. $row["last_name"] ." "
. $row["email"] ." " ;
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
Try this one, if it works for you..
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";
You might also be intrested in the foreach way.
Foreach would be quicker in larger data sets, but this is very similar to Narendrasingh Sisodia answer.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
if($result->num_rows > 0){
foreach($result as $row){
echo "<tr>";
echo "<td>". $row["first_name"] . "</td>";
echo "<td>". $row["last_name"] . "</td>";
echo "<td>". $row["email"] . "</td> " ;
echo "</tr>";
}
}
echo "</table>";

Can't get multiple result in td

I have this problem in displaying the result in td inside table. I want to display all rows coming from the database.
I have code but it results only one row.
<table class="table table-striped table-bordered table-hover" id="dataTables">
<thead>
<tr>
<th>ID</th>
<th>Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost", "root", "", "trackingsys");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * from role" ;
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['RoleName'] . "</td>";
echo "<td>" . $row['RoleDesc'] . "</td>";
echo '</tr>';
}
mysqli_close($con); ?>
</tbody>
</table>
I edited this code and put the ending tag </td>. But still it results only one row.

Categories