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

880

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?

EDIT

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">
 <?php
  $result  = mysql_query("SELECT * FROM vetrina");
  $element = mysql_fetch_assoc($result);
  $value  = array(array_keys($element));
  $i    = 0;
   foreach($value as $rowname){

    if($i==0){
     $i++;
     continue;
    }
   ?>
    <div >
     <span >
      <input name="rowpost[]" type="checkbox" aria-label="...">
     </span>
     <span id="basic-addon3">
      <i  aria-hidden="true">
      </i>
      <?php
       echo $rowname;
      ?>
     </span>
    </div>
   <?php
   }
 ?>
 <div >
  <div >
   <span >
    <input type="checkbox" name="chkaddshcs">
    Altra Vetrina
   </span>
   <input type="text" name="addrowname" placeholder="Aggiungi Vetrina">
  </div><!-- /input-group -->
 </div><!-- /.col-lg-6 -->
 </form>
502

Answer

Solution:

MySQL has a query to show all the column names:

SHOW COLUMNS FROM `vetrina`

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.

People are also looking for solutions to the problem: php - List name and link directories

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.