PHP MySql Result as Table in html - php

Im trying to list the result from MySQl results as a Table in my HTML.
My PHP file:
$stmt = $this->get('database_connection')
->executeQuery(
'SELECT * FROM members'
);
while (false !== ($objMember = $stmt->fetch(\PDO::FETCH_OBJ)))
$template->listTable= implode(', ', $objMember['firstname']);
return $template->getResponse();
And in HTML:
block('content'); ?>
<p><?= $this->listTable ?></p>
<?php $this->endblock(); ?>
But I dont get any reults in the html file. What is the correct way to list all results from SQL to a Table?

I don't know if it is the right way or not but it works as this :
I create some html table
<table class="table">
<thead class=" text-primary">
<th>table header</th>
<th>table header 2</th>
</thead>
<tbody>
In here my PHP begins
<?php
$query = mysqli_query($mysql_connection, "SELECT * FROM tbale WHERE conditions");
while ($array = mysqli_fetch_array($query)) {
$k_exmaple_1 = $array['column1'];
$k_exmaple_2 = $array['column2'];
echo "
<tr>
<td>$k_exmaple_1</td>
<td>$k_exmaple_2</td>
</tr>
";
}
?>
PHP Ends
I connected the table in MySQL, and selected my rows then print it with echo in html format.
</tbody>
</table>
Table ends

Related

MYSQL query in PHP is deleting all the rows in my table

I'm working on a PHP script that uses delete and I only want to delete 1 row. But everytime I tried to delete 1 row, the SQL executes but deletes the entire rows in the table.
The following is my PHP script
<table>
<tr>
<th>No.</th>
<th>Publisher Code</th>
<th>Publisher Name</th>
<th>City</th>
<th> </th>
</tr>
<!-- PHP FETCH/RETRIEVE FROM DB -->
<?php
include_once 'dbconnect.php';
$sql = mysqli_query($conn, "SELECT * FROM tbl_publisher");
$ctr = 1;
$record = mysqli_num_rows($sql);
if ($record > 0) {
while ($record = mysqli_fetch_array($sql)) {
?>
<tr>
<td> <?php echo $ctr++ ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_CODE']; ?> </td>
<td> <?php echo $record['TBL_PUBLISHER_NAME']; ?> </td>
<td> <?php echo $record['CITY']; ?> </td>
<td id="actions">
<center>
Delete
</center>
</td>
</tr>
<!-- closing tag for php script -->
<?php
}
}
?>
<!-- -->
</table>
and below is my delete.php
if(isset($_GET['del-pub']))
{
$row = intval($_GET['del-pub']);
$sql = mysqli_query($conn,"DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;");
if ($sql) {
header("Location: publisher.php");
}
else {
echo "<script>alert('Publisher was not deleted.');</script>";
header("Location: publisher.php");
}
}
I want to know how to delete only the 1 row. But it keeps deleting the entire rows in the table.
After dumping the contents of $sql, I found out that my sql syntax is the culprit.
Since TBL_PUBLISHER_CODE uses char as ID. Instead of doing "="
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE = $row;
I used
DELETE FROM tbl_publisher WHERE TBL_PUBLISHER_CODE LIKE '".$row."'

Issue with using php and sql to add data to datables

So I'm trying to use php and mysql to put data in my datatables on my website. I wrote some code at the top of my file to confirm that I am accessing my database correctly. The table is displayed correctly when i manually entered some data, but with my php code, the table says "No data available in table". Any thoughts on whats wrong with my php?
$q = "SELECT tickets.ticket_id, tickets.section, tickets.row, tickets.price, users.first_name as first_name, users.last_name as last_name
FROM tickets
LEFT JOIN users
ON tickets.seller_id = users.umid
Where(tickets.game_id = '$g')";
$r = #mysqli_query ($dbc, $q);
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Section</th>
<th>Row</th>
<th>Price</th>
<th>Seller</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '
<tr>
<td>'.$row["section"].'</td>
<td>'.$row["row"].'</td>
<td>'.$row["price"].'</td>
<td>'.$row["first_name"]. " ". $row["last_name"]. '</td>
</tr>
';
}
?>
</tbody>
</table>
This is what the table looks like
Link to the webpage
Figured out the problem. I looped through my row earlier in my code, so when i wanted to put the info in the table, it was at the end of the array.

How to add a Checkbox beside each row in table

How to make so a checkbox is added beside each row in a dynamic table? So if i get 5 result from databse i want to display 5 checkbox next to these results.
<table border="2">
<tr>
<th>title</th>
</tr>
<?php
include 'database.php';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
echo '<td>'.$row['title'].'</td>';
<input type = 'checkbox' name = '$row[title]'>
echo '<tr>';
}
?>
</table>

Mysql and php w/ html database issue

I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>

Got stuck with populating mysql table in html table [duplicate]

Here the value of $table will be passed from another file and it will be a table name in a database.
With that table name ($table) am trying to fetch its column names and all the values in the table and make the table look like an actual one we see in phpMyAdmin.
As a result of the code below. I can only get the column names. But not the data. Please tell me what should i do to perform the task of showing the datas in the table
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php
//echo"select * from $table";
$qq=mysql_query("show columns from $table");
if(mysql_num_rows($qq)>0){
//$i=1;
echo '<tr>';
while($rs = mysql_fetch_row($qq))
{
$sel=mysql_query("select * from $table");
$fetch=mysql_fetch_object($sel);
//while($fetch=mysql_fetch_object($sel))
//{
?>
<td><?php echo $fetch->$rs[0];?></td>
<?php
//}
//$i++;
}
echo '</tr>';
}
else
{
?>
<tr>
<td colspan="11">No data to display</td>
</tr>
<?php
}
?>
</tbody>
</table>
Yes only columns will be printing because you are printing $rs[0];?> where $rs holds object for mysql_query qq and it holds your column query only not
"select * from $table"
this.
Why dont you try displaying columns as a single row in html table and then try displaying data from other php set with seperate query structure .
Hope this way it helps.
I coudn't fix your code because it was getting ugly, so i remade it:
php:
function mysql_fetch_all($res) {
$result = array();
while ($row = mysql_fetch_row($res)) {
$result[] = $row;
}
return $result;
}
function getColumnsAndData($table) {
$table = mysql_real_escape_string($table);
$q1 = mysql_query("show columns from $table");
$q2 = mysql_query("select * from $table");
return array(mysql_fetch_all($q1), mysql_fetch_all($q2));
}
list($columns, $data) = getColumnsAndData($table);
?>
html
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php foreach ($columns as $column): ?>
<td><?php echo $column[0] . ' ' . $column[1] ?></td>
<?php endforeach; ?>
<tr>
</thead>
<tbody>
<?php if (count($data) > 0): ?>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value ?></td>
<?php endforeach; ?>
<tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td>No data to display</td>
</tr>
<?php endif; ?>
</tbody>
</table>

Categories