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>
Related
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."'
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
I have the following code:
$sql = "SELECT * FROM Tickets WHERE stat='Open'";
$result = mysql_query($sql);
mysql_close($con);
?>
<!DOCTYPE>
<html>
<body>
<table class="striped">
<tr class="header">
<td>Username</td>
<td>Title</td>
<td>Description</td>
<td>Admin Name</td>
<td>Category</td>
<td>Status</td>
<td>Urgency</td>
<td>Time In</td>
<td> </td>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>".$row[username]."</td>";
echo "<td>".$row[title]."</td>";
echo "<td>".$row[description]."</td>";?>
<td><select>
<?php
echo "<option value'".$row[admin_name]."'>".$row[admin_name]."</option>";
$sql = mysql_query("SELECT username FROM Users WHERE user_type='admin'");
while ($u = mysql_fetch_array($sql)){
echo "<option value='".$u['username']."'>".$u['username']."</option>";
}
?>
</select></td>
<?php
echo "<td>".$row[category]."</td>";
echo "<td>".$row[stat]."</td>";
echo "<td>".$row[urgency]."</td>";
echo "<td>".$row[time_in]."</td>";
echo "<td><a href='close.php'>Close Ticket</a></td>";
echo "</tr>";
}
?>
</table>
<a href='update.php'>Update</a>
</body>
</html>
I have two links on this page. Both of them need to update a SQL database. The Close ticket link needs to just update the single row, while the update link should update all of them. I am not sure how to get the info from one php to the next. It seems like you can put the individual row information into a Post array for the close ticket link, but I am not sure how. For the update link it needs to take the value of the dropdown in the table and change the admin_name field to that value.
I'm really stuck on this question, I have given it my all and I still cant work out whats wrong, even my friends have attempted to help me, I'm beginning to think its unsolvable... Here's what is wants
Create a PHP script, named task3a.php, that retrieves the names and id of each driver, and outputs the
information differently to the previous two tasks. Instead of displaying a table, the output should contain
an HTML form. The form should contain a submit button and a drop-down list input. The drop-down
input should contain the driver names, and the form should submit via the GET method to task4.php
when the submit button is pressed. Name the select input driver.
And here's what I've got -
<!DOCTYPE HTMl>
<html>
<body>
<?php
try {
$dbhandle = new PDO('mysql:host=<...>.ac.uk;dbname=user','user','pass');
} catch (PDOExeption $e) {
die('Error Connecting to Database: ' . $e->getMessage());
}
$driver = 'SELECT forename, surname, d.nationality, name FROM Drivers d JOIN Teams t ON d.id = t.id';
$query = $dbhandle->prepare($driver);
if ($query->execute() === FALSE ) {
die('Error Running Query: ' . implode($query->errorInfo(), ' '));
}
$query->execute();
$result = $query->fetchAll();
?>
<table>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Nationality</th>
<th>Team</th>
</tr>
<?php foreach($result as $row) { ?>
<tr>
<td><?php echo $row['forename']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['nationality']; ?><td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php } ?>
</table>
<form action='task3a.php' method='GET'>
<select name=''driver>
<?php foreach($results as $row) { ?>
<option value='<?php echo $row['id'];?>' > <?php echo $row['name']; ?> </option>
<?php } ?>
</select>
</form>
</body>
</html>
It's giving me a table with everything in it at the moment and then under it a drop down box with nothing in it, I'm so confused
$result = $query->fetchAll();
^--- no S
<?php foreach($result as $row) { ?>
^---no S
<?php foreach($results as $row) { ?>
^----where did this S come from?
Consider changing this
<select name=''driver>
to
<select name='driver'>
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>