valbuxvb valbuxvb - 1 year ago 49
MySQL Question

How do I put all column names in a php array?

I have a table named showcase and I want the user inserting his products in this table. The point is that I would like that the user could add showcase argument like "sale" or whatever he wants. To do that I thought he could insert the showcase argument in a form and then add it like a column in the showcase table. If I don't know column names how can I show them?


Ok, I used this code, I skipped the first column because it's the id one... It doesn't show me nothing but the part after the foreach... Do you know what could it be?

<form method="post" action="move_showcase.php">
$result = mysql_query("SELECT * FROM vetrina");
$element = mysql_fetch_assoc($result);
$value = array(array_keys($element));
$i = 0;
foreach($value as $rowname){

<div class="input-group">
<span class="input-group-addon">
<input name="rowpost[]" type="checkbox" aria-label="...">
<span class="input-group-addon" id="basic-addon3">
<i class="fa fa-minus-circle" style="color:#d9534f; cursor:pointer;" aria-hidden="true">
echo $rowname;
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" name="chkaddshcs">
Altra Vetrina
<input type="text" class="form-control" name="addrowname" placeholder="Aggiungi Vetrina">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->

Answer Source

MySQL has a query to show all the column names:


You can loop the return value like normal table rows.

$ergebnis = mysql_query('SHOW COLUMNS FROM `vetrina`');

while ( $array = mysql_fetch_array( $ergebnis ) ){
  $columns[] = $array['Field'];

// remove first name
$columns = array_slice( $columns, 1 );

foreach( $columns as $column ){
 echo $column;
 echo "\r\n";

I would recommend you to switch to PHP PDO or MySQLi, because php_mysql is outdated and insecure.

Also reading results is more powerful.