Reputation: 77
I want to display all rows/data from a MySQL table without knowing the column names.
I have the table name but not the column names, and I want to build a grid of the data using the table HTML attribute. I was thinking of using the schema but no idea where to start, any suggestions ?
Upvotes: 0
Views: 5661
Reputation: 2050
To get the table structure, use this SQL query:
SHOW COLUMNS FROM table_name;
From there you can then make a query that grabs all data from the table and display it as you want.
Upvotes: 1
Reputation: 80
On your select statement you could do "select * from table_name". Then, you go to your for loop() in php and can do more less like this:
$query = mysqli_query($your_connection_name, $your_sql_query);
Then, place a while loop:
<?php
while($row = mysqli_fetch_array($query)){
$item_one = $row[0];
$item_two = $row[1];
$item_three = $row[2];
? > *//end php*
your html code here (table etc)
< ? php *//start php again*
} *//end while loop*
?> *//end php*
hope it helps.
Upvotes: 0
Reputation: 504
This is actually an easy task to achieve with the MYSQLI_ASSOC function as it will put in the db field name as the array key. Take a look at this example:
$query = "SELECT * FROM `MyTable`";
if ($result = $mysqli->query($query)) {
echo '<table>';
foreach($result->fetch_all(MYSQLI_ASSOC) as $row) {
foreach($row as $key => $value) {
echo '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
}
}
echo '</table>';
}
This would print all the tables key and associated values
Upvotes: 0
Reputation: 776
it may be helpful for you
$response = mysql_query('select * from table_name');
while($result = mysql_fetch_assoc($response))
{
foreach($result as $key => $value)
{
echo $key.'--'.$value;
}
}
Upvotes: 0
Reputation: 41
SELECT * FROM table_name
;
that should give you all the data including all rows.
Upvotes: 4