简体   繁体   中英

Can an array of database table column headings be created?

I have a table col-heading whose columns are h1 to h40 and I want to store data in them by using an array of these column headings. Is it possible?

$query = mysql_query("INSERT INTO col_heading(h1---hn) VALUES ('$values')", $connection );

The question is a little unclear but to get all the column names from a particular table you can try something like this. You will note that it uses mysqli rather than the now deprecated mysql functions.

$dbhost = 'localhost';
$dbname = 'xxx';
$dbpwd = 'xxx';
$dbuser = 'xxx'; 

$db =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );


/* Get all columns from a particular table */
$sql="select `column_name` as 'column' 
        from `information_schema`.`columns` 
        where `table_schema`=database() and `table_name` = 'col_heading';";


$results=$db->query( $sql );
$columns=array();

while( $rs=$results->fetch_object() ){
    $columns[]=$rs->column;
}

$db->close();
$db=null;

echo '<pre>',print_r($columns),'</pre>';

Now you have an array of column names you should be able to use for the next operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM