繁体   English   中英

是否可以创建数据库表列标题的数组?

[英]Can an array of database table column headings be created?

我有一个表col-heading其列为h1到h40,我想通过使用这些列标题的数组在其中存储数据。 可能吗?

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

这个问题还不清楚,但是要从特定表中获取所有列名,可以尝试如下操作。 您会注意到,它使用mysqli而不是现在不推荐使用的mysql函数。

$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>';

现在,您已经可以使用列名称数组进行下一个操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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