I'm in need of some help. I'm working in dreamweaver and I'm trying to insert values from my MySQL database into a table on my HTML page.
Dreamweaver generated these variables for me from the server behaviours
mysql_select_db($database_connection, $connection);
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
$row_mwAcc = mysql_fetch_assoc($mwAcc);
$totalRows_mwAcc = mysql_num_rows($mwAcc);
Now what I need help with is what to put into the while loop for my PHP script, this is what I have so far
<table class="table table-bordered">
<?php while (): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endwhile; ?>
</table>
You need to write your fetch command within the while loop itself.
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
while($row_mwAcc = mysql_fetch_assoc($mwAcc)) {
?>
<tr>
<td><?php echo $row_mwAcc['id'] ?></td>
</tr>
<?php } ?>
</table>
With an update to use PDO you can change the while loop into a foreach on the query result. PDO should be used over mysql_ interface methods, due to deprecation: Why shouldn't I use mysql_* functions in PHP?
<?php
$dbh = new PDO('mysql:host=...;dbname=...', $user, $pass);
$results = $dbh->query('SELECT * FROM accounts');
?>
<table class="table table-bordered">
<?php foreach ($results as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endforeach; ?>
</table>
Related
I create a code to display from a table
$sql = "SELECT * FROM table";
$stmt = $dbcon->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
to display the result, I use the following ode:
<?php foreach($result as $readrow){ ?>
<div class="table-row">
<td><?php echo $readrow['id'];?></td>
<td><?php echo $readrow['area'];?></td>
<td><?php echo $readrow['color'];?></td>
<?php } ?>
Is there any way, the table can be displayed with the header?
There are many duplicated questions but all answers are beyond any reason, offering complicated solutions involving running extra queries etc.
While the solution is right here: when using fetchAll(), you already have all the column headers in the $result variable
$headerNames = $result ? array_keys($result[0]) : [];
now you can foreach over $coulmnNames to get the table header and the foreach over $result to display the results.
<table class='table'>
<tr>
<?php foreach($coulmnNames as $name): ?>
<th><?= $name ?></th>
<?php endforeach ?>
</tr>
<?php foreach($result as $row){ ?>
<tr class="table-row">
<?php foreach($result as $value){ ?>
<td><?= $value ?></td>
</tr>
<?php endforeach ?>
</table>
How do I make a member list with PHP and MySQL?
I have user accounts, login and that stuff. How do I make a page that shows all the members?
I can provide code!
NOTE: Your question is not clear. You have not included any code or query snippet. I will try my best to answer your question.
As per my knowledge I think your users are nothing but the members. Just write a query to pull all the users from the users table and display.
NOTE : I will use mysqli_* function without any escaping please help yourself.
<?php
include_once 'db_connect.php'; /Line to include the database connection file, which had $link as resource */
$membersQuery = mysqli_query($link, "SELECT * FROM users");
$members = array();
if(mysqli_num_rows($membersQuery) > 0){
while($row = mysqli_fetch_assoc($membersQuery)){
$members[] = $row; //Get all the members row by row and store in $members array
}
}
?>
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?
if(count($members) > 0){
foreach($members as $member){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Even while looping in table you can use the following command
<tbody>
<?
if(mysqli_num_rows($membersQuery) > 0){
while($member = mysqli_fetch_assoc($membersQuery)){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
I need to be able to display the contents of my database but I am not sure how to do this using sqli, any help provided will be appreciated
<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
$servername = "server";
$username = "username";
$password = "password";
$dbname = "sql1702520";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
I tried to use a mysql query bellow to extract the data from the database, however i have been told that it is not possible to use mysql and sqli
/*
$result = mysql_query("SELECT * FROM sql1702520 ");
*/
$conn->close();
?>
Html to display the database
<html>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
Tried using this to display the table data however mysql would be required to do it this way
while( $row1 = mysqli_fetch_assoc( $result ) ){
foreach ($row1 as $row){
?>
<tr>
<td><?php $row['id'] ?></td>
<td><?php $row['hname'] ?></td>
<td><?php $row['himage'] ?></td>
<td><?php $row['hdesc'] ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
</body>
</html>
assuming that your tablename (what looks suspiciously like your database name) for mysqli you would have to use the mysqli_query function to send your query.
$result = mysql_query($conn,"SELECT * FROM sql1702520 ");
and mysqli_fetch_assocfor your loop
$row = mysqli_fetch_assoc($result);
you can ditch the for-each loop. the while loop is sufficient.
for further help an error message would help.
Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
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>