<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>
Related
I have a database called simple_stall with table order_detail which have 4 columns ID Name Ordered_Item Quantity...currently after user submit their order, they'll be redirected to a page called order_detail.php...this page will show all ordered item in table with header ID Name Ordered_Item Quantity
now, when user click on someone's name from the table, i want to redirect user to a new page called view_more.php which will show the item ordered by the user however, nothing showed in the page.
This is my code:
index.php
<div class="container">
<form action="insert_data.php" method="POST">
<div>
<input type="text" name="Name" placeholder="Name">
</div>
<div>
<input type="text" name="Order" placeholder="Order">
</div>
<div>
<input type="text" name="Quantity" placeholder="Quantity">
</div>
<div>
<button type="submit" name="submit">Send Order</button>
</div>
</form>
</div>
insert_data.php
if (isset($_POST['submit']))
{
include_once 'dbh.php';
// Escape user inputs for security
$name = mysqli_real_escape_string($connection, $_POST['Name']);
$order = mysqli_real_escape_string($connection, $_POST['Order']);
$quantity = mysqli_real_escape_string($connection, $_POST['Quantity']);
// attempt insert query execution
$sql = "INSERT INTO order_detail (Name, Ordered_Item, Quantity) VALUES ('$name', '$order', '$quantity')";
if(mysqli_query($connection, $sql))
header("Location: ./order_detail.php?status=ordered");
else
echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
// close connection
mysqli_close($connection);
}
else
{
header("Location: ./index.php?status=failed");
exit();
}
order_detail.php
<body>
<table>
<tr>
<th width="30px">No</th>
<th width="30%">Name</th>
<th width="30%">Ordered Item</th>
<th width="50px">Quantity</th>
</tr>
<?php
include_once 'dbh.php';
$query = "SELECT * FROM order_detail"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
$name = $row['Name'];
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection); //Make sure to close out the database connection
?>
view_more.php
if (isset($_POST['Name']))
{
include_once 'dbh.php';
$name = $row['Name'];
$query = "SELECT * FROM order_detail WHERE Name = $name";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
It will not show,
because on view_more.php you have if (isset($_POST['Name'])) which will never be true since you are not using $_POST on view_more.php, you are using <td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td> you are using normal link so replace it with this code
if (isset($_GET['id']))
{
include_once 'dbh.php';
$name = $_GET['id'];
$query = "SELECT * FROM order_detail WHERE Name = '$name'";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
and you should be good to go, however, I highly recommend you to use proper php framework.
When generating the link to the view_more.php page, you're are injecting a GET param named id:
<a href='view_more.php?id=$name'>
And on the view_more.php page, you are testing for a POST param called name:
if (isset($_POST['Name']))
fix this to
if (isset($_GET['id']))
By the way, your code is really, really ugly. there are a tons of things done wrong :
unescaped params in SQL query : you are exposed to SQL injections which is a severe security breach.
coupled PHP and HTML scripts : take a look at Separation of Concerns and MVC Design pattern
I have created simple table and stored an images with base64 as a TEXT ,
when I tried to fetch all data's from database, it works well, Except the images is displaying on website like below code.
LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc
php code.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn -> connect_error){
die("my connection faild" . $conn -> connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while($row = $result-> fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
base64_encode($row["image"]) .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn -> close();
?>
</table>
</body>
</html>
any solution how do display the images from db for my current problem?
Decode your image string in data:image/gif;base64
<img src="data:image/gif;base64,' . $row["image"] . '" />
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn->connect_error){
die("my connection faild" . $conn->connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
'<img src="data:image/gif;base64,' . $row["image"] . '" />' .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn->close();
?>
</table>
</body>
I have this line in my syntax if (query) which I thought would check if the query was null, however my query returns 0 rows and the header row is still displayed.
What is the proper way to ensure the query returns rows before you display the table?
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'ServerName';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT blue, green, red, purple, pink from colors";
$db->setQuery($query);
$query = $db->loadObjectList();
if (query)
{
?>
<div id="dvData">
<table id="example" border="1">
<caption><strong style="font-size:25px;"><?php echo $startdate ?> - <?php echo $enddate ?></strong></caption>
<thead>
<tr>
<th bgcolor="#00FF00">Blue </th>
<th bgcolor="#00FF00">Green </th>
<th bgcolor="#00FF00">Red </th>
<th bgcolor="#00FF00">Purple </th>
<th bgcolor="#00FF00">Pink </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->Blue . "</td>";
print "<td>" . $res->Green . "</td>";
print "<td>" . $res->Red . "</td>";
print "<td>" . $res->Purple . "</td>";
print "<td>" . $res->Pink . "</td>";
print "</tr>";
}
}
?>
</table>
</div>
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>";
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>";