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;
?>
Related
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);
}
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>";
?>
This is the my code and I am trying to store the value into the Array.
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
echo $row['Field'];
echo "<br/>";
}
I am trying to do this $ar[]=$row; but there is nothing display !!
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
ar[] = $row;
echo "<br/>";
}
for($i = 0, $i<12, $i++) {
echo ar['field'][0];
}
Nothing Display in the output
You can do following way to store column name into array. Read comment after every line
For PDO
$rs = $cid->query('SELECT * FROM commissions');// your query
for ($i = 0; $i < $rs->field_count(); $i++) {/// count nu of column
$col = $rs->fetch_fields()($i);//Returns metadata for a column in a result set
$columns[] = $col['name'];// get name from metedata
}
print_r($columns);
For mysqli
$rs = $cid->query('SELECT * FROM commissions');// your query
$col = $rs->fetch_fields();//Returns metadata for a column in a result set
foreach ($col as $val) {
$columns[]=$val->name;
}
print_r($columns);
I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}
Is this OK ?
$i = 0;
while ($row = mysql_fetch_array($result))
{
$resultset[] = $row;
$columns[] = mysql_fetch_field($result, $i);
}
Then when trying to print
<tr><th><?php echo $columns[0] ?></th><th><?php echo $columns[1] ?></th></tr>
I got an error
Catchable fatal error: Object of class stdClass could not be converted to string
Try the mysql_fetch_field function.
For example:
<?php
$dbLink = mysql_connect('localhost', 'usr', 'pwd');
mysql_select_db('test', $dbLink);
$sql = "SELECT * FROM cartable";
$result = mysql_query($sql) or die(mysql_error());
// Print the column names as the headers of a table
echo "<table><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);
echo "<th>{$field_info->name}</th>";
}
// Print the data
while($row = mysql_fetch_row($result)) {
echo "<tr>";
foreach($row as $_column) {
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Use mysql_fetch_assoc to get only an associative array and retrieve the column names with the first iteration:
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
}
$resultset[] = $row;
}
Now you can print the head of your table with the first iteration as well:
echo '<table>';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
}
$resultset[] = $row;
echo '<tr><td>'.implode('</td><td>', $rows).'</td></tr>';
}
echo '</table>';
You want to look at
mysql_fetch_assoc
Which gives each row as an associative key => value pair where the key is the column name.
Documentation here
Here is a more modern solution with ...i:
$db_link = #mysqli_connect($server, $user, $pass, $db) or die("Connection failed");
$spalten = ['pc_name', 'pc_type', 'pc_snr', 'pc_bj', 'mo_port_1', 'mo_snr_1', 'mo_bj_1', 'mo_port_2', 'mo_snr_2', 'mo_bj_2'];
$abfrage = "SELECT " . implode(',', $spalten) . " FROM hardware order by pc_name";
$liste = mysqli_query($db_link, $abfrage);
$spalten = mysqli_num_fields($liste);
while ($werte[] = mysqli_fetch_array($liste, MYSQLI_ASSOC)) {}
<table class="display" id="table" style="width:100%">
<thead>
<tr>
<?php
for ($i = 0; $i < $spalten; $i++) {
$feldname[$i] = mysqli_fetch_field_direct($liste, $i)->name;
echo '<th>' . ucfirst($feldname[$i]) . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < mysqli_num_rows($liste); $i++) {
?>
<tr>
<?php for ($j = 0; $j < $spalten; $j++) {
?>
<td><?php
echo $werte[$i][$feldname[$j]];
?>
</td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
Though it's deprecated and no longer in PHP 7 you can avoid having to use an object by using the function mysql_field_name instead which returns a string.