i need help.. im trying to echo all the data from my database but from the last to the first.
im a college student and i need ur help.
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) { // my lecture ask me to echo 5 data only
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
that is my code for now, and its only echoing from the first table to the last. my lecture told me i need to echo from the last.
edit :
i think i haven't give all the information.. sorry, this is the first time im here :(.
<?php
$i = 1;
include_once("function/helper.php");
include_once("function/koneksi.php");
$query = mysqli_query($koneksi, "SELECT * FROM transaksi ");
$pemilik = mysqli_fetch_assoc($query);
?>
<?php
$i = 1;
$sql = "SELECT mutasi, waktu_tanggal,tujuan FROM transaksi WHERE user_id='$user_id' ORDER BY waktu_tanggal";
$result = mysqli_query($koneksi, $sql);
if(mysqli_num_rows($result) == 0) {
?>
<h1>Anda belum melakukan transaksi apapun</h1>
<?php
}else {
echo "<br>";
echo "<table border='1'>";
?>
<tr>
<th>Mutasi</th>
<th>Waktu</th>
<th>Keterangan</th>
</tr>
<?php
while (($row = mysqli_fetch_assoc($result)) && ($i < 6)) {
echo "<tr>";
foreach ($row as $field => $value) { line like this: foreach($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
$i = $i + 1;
}
echo "</table>";
}
?>
that is the entire page.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
The sql [ORDER BY] command is used to sort the result set in ascending or descending order.
SELECT column1, column2 FROM table_name ORDER BY column1 DESC;
Try php function mysqli_fetch_all () --- >> you don't need a while-loop.
Then create a new array and populate it with the required number of records.
Like this:
$query = "SELECT TOP 5*FROM `MyTable`";
// Or alternative query: "SELECT*FROM `MyTable LIMIT 5`
$result = mysqli_query($connection, $query);
if($result != 0) {
$newDataArray = mysqli_fetch_all($result, MYSQLI_BOTH);
}
Related
I'm trying to make a code that permits the user to show any table in my database in a php page.
I tried with that code:
<?php
include('connection_db.php');
$table = $_POST['table'];
$sql1 = "SELECT * FROM $table";
$sql2 = "SELECT count(*)
FROM information_schema.columns
WHERE table_name = '$table'";
$sql3 = "SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'$table'";
$result = mysql_query($sql1);
$column = mysql_query($sql2);
$ncolumn= mysql_query($sql3);
echo "<table width='100%' border='1'>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
for ($i = 0; $i <= $column; $i++) {
echo "<td>". $row['$ncolumn'] ."</td>";
}
echo "</tr>";
echo "<br/>";
}
echo "</table>";
?>
It should show the entire table with his records, but it shows only a lot of empty lines
$table = $_POST['table'];
$mysqli = new mysqli(_DB_SERVER_,_DB_USER_,_DB_PASSWD_,_DB_NAME_);
$mysqli->set_charset("utf8");
if (mysqli_connect_errno()) {
$Message = "Connect failed: %s\n" . $mysqli->connect_error;
exit();
}
if ($result = $mysqli->query("SHOW TABLES LIKE '".$table."'")) {
if($result->num_rows == 1) {
$Tableexists = "yes";
} else {
$Tableexists = "no";
}
} else {
$Tableexists = 0;
}
echo $Tableexists;
I want to read out data from an sql-database an show them in a table. This works well. Now, I would like to show only those columns with at least one value in it and not the empty ones (containing NULL, 0, empty string). This works with the following example:
enter code here
<TABLE width="500" border="1" cellpadding="1" cellspacing="1">
<?php
$query = mysql_query("SELECT * FROM guestbook", $db);
$results = array();
while($line = mysql_fetch_assoc($query)){
$results[] = $line;
$Name = array_column($results, 'Name');
$Home = array_column($results, 'Home');
$Date = array_column($results, 'Date');
$Emptycolumn = array_column($results, 'Emptycolumn');
$Comment = array_column($results, 'Comment');
$City = array_column($results, 'City');
}
echo "<TR>";
if(array_filter($Name)) {echo "<TH>Name</TH>";}
if(array_filter($Home)){echo "<TH>Home</TH>";}
if(array_filter($Date)){echo "<TH>Date</TH>";}
if(array_filter($Emptycolumn)){echo "<TH>Emptycolumn</TH>";}
if(array_filter($Comment)){echo "<TH>Comment</TH>";}
if(array_filter($City)){echo "<TH>City</TH>";}
echo "</TR>";
$query = mysql_query("SELECT * FROM guestbook", $db);
while($line = mysql_fetch_assoc($query)){
echo "<TR>";
if(array_filter($Name)) {echo "<TD>".$line['Name']."</TD>";}
if(array_filter($Home)) {echo "<TD>".$line['Home']."</TD>";}
if(array_filter($Date)) {echo "<TD>".$line['Date']."</TD>";}
if(array_filter($Emptycolumn)) {echo "<TD>".$line['Emptycolumn']."</TD>";}
if(array_filter($Comment)) {echo "<TD>".$line['Comment']."</TD>";}
if(array_filter($City)) {echo "<TD>".$line['City']."</TD>";}
echo "</TR>";
}
?>
</TABLE>
Since the column-names of my table are highly variable (depending on the query), the table is generated by looping through the result-array, first the column-names, then the values in the rows:
enter code here
$sql = "SELECT DISTINCT $selection FROM $tabelle WHERE
$whereclause"; //will be changed to PDO
$result = mysqli_query($db, $sql) or die("<b>No result</b>"); //Running
the query and storing it in result
$numrows = mysqli_num_rows($result); // gets number of rows in result
table
$numcols = mysqli_num_fields($result); // gets number of columns in
result table
$field = mysqli_fetch_fields($result); // gets the column names from the
result table
if ($numrows > 0) {
echo "<table id='myTable' >";
echo "<thead>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th>";
for($x=0;$x<$numcols;$x++){
$key = array_search($field[$x]->name, $custom_column_arr);
if($key !== false){
echo "<th>" . $key . "</th>";
}else{
echo "<th>" . $field[$x]->name . "</th>";
}
}
echo "</tr></thead>";
echo "<tbody>";
$nr = 1;
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $nr . "</td>";
for ($k=0; $k<$numcols; $k++) { // goes around until there are no
columns left
echo "<td>" . $row[$field[$k]->name] . "</td>"; //Prints the data
}
echo "</tr>";
$nr = $nr + 1;
} // End of while-loop
echo "</tbody></table>";
}
}
mysqli_close($db);
Now, I tried to integrate the array_column() and array_filter()-blocks of the example above into the loops, but unfortunately, it didn´t work. I´m sure, this is easy for a professional and I would be very grateful, if someone could help me with this problem!
Thank you very much in advance!!
I would to like to create a dynamic table that the rows and columns of the table depends on the data in mysql data. Please see below my codes.
<?php
require "connect/db.php";
$query = mysqli_query($mysqli, "SELECT COUNT(level) level, shelf_no, bin_id FROM location_bin WHERE rack_id =1 GROUP BY shelf_no");
while($res=mysqli_fetch_array($query)){
$col = $res['level']; //col
$row = $res['shelf_no']; //rows
echo "<table border='1'>";
$i = 0;
while ($i < $col){
if ($i==$col){
echo "<tr>";
}
echo "<td>".$res['bin_id']."</td>";
$i++;
}
echo "</tr>";
echo "</table>";
}
?>
What I want is to display A-1-01 up to A-1-06 in the first row then A-2-01 to A-2-03 on the second row. Note that the data is dynamic.
Can you try this code . It works. change db.php path as your system path.
require "db.php";
$query = mysqli_query($conn, 'SELECT level, shelf_no, bin_id FROM location_bin ORDER BY shelf_no asc, level asc ');
echo "<table border='1'>";
echo "<tr>";
$i=0;
while($res = mysqli_fetch_assoc($query)) {
$row=$res['shelf_no'];
if ($i < $row){
$i=$row;
echo "</tr><tr>";
}
echo "<td>".$res['bin_id']."</td>";
}
echo "</table>";
?>
I have several columns in my database and I would like to print out all the column names, i.e. the names of the people in the database.
I'm having trouble with selecting ONLY the column names, this piece of code will run the loop but only the $i will print, not the actual names of the columns. Any advice? Thanks
$query = mysql_query("SELECT * FROM People");
$i = 1;
while($row = mysql_fetch_assoc($query)) {
$name = $row['query'];
if ($count > '1') {
?>
<table>
<tr>
<td><p><b><?php print( $name . "</p></td></tr>");
$i = $i + 1;
?>
P.s.
I tried with:
select COLUMN_NAME from Names.COLUMNS where TABLE_NAME = 'Names' but this won't work either, it tells me: access denied, don't really know why
Here is the code to get column names for your table.
$sql = "SHOW COLUMNS FROM your-table";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row['Field']."<br>";
}
But, as you get access denied. It means you do not have privilege to access table metadata.
So you can use either,
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
// Here $columns is array and it contains all column name
}
}
I think you should try this:
$query = "SELECT * FROM People";
$result = mysqli_query( $conn, $query );?>
<table>
<?php
while($row = mysqlsi_fetch_assoc( $result ) ){
?>
<tr>
<td><p><?php print( $row->id );?></p></td>
<td><p><?php print( $row->name );?></p></td>
<td><p><?php print( $row->lastname );?></p></td>
</tr>
<?php
} ?>
</table>
Assuming,your SQL table has these fields: id, name, lastname.
Inside your while loop you could use
foreach($row as $column => $value){
printf("Column: %s has value: %s\n", $column, $value);
}
As mysql_fetch_assoc returns an associative array so you want the key => value information.
HTH
Use mysql_field_name function in php to get column name
$query = mysql_query("SELECT * FROM People");
$i = 1; $j=0; // I have declared $j incrementor
while($row = mysql_fetch_assoc($query)) {
$name = $row['query'];
$columnname = mysql_field_name($query, $j); // This gives the column name of table.
if ($count > '1') {
?>
<table>
<tr>
<td><?php echo $columnname; ?></td>
<td><p><b><?php print( $name . "</p></td></tr>");
$i = $i + 1;
$j = $j + 1;
?>
Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.