gpt4 book ai didi

php - 如何将表字段(列)名称存储到数组中?

转载 作者:行者123 更新时间:2023-11-29 07:34:17 24 4
gpt4 key购买 nike

这是我的代码,我正在尝试将值存储到数组中。

$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {

echo $row['Field'];
echo "<br/>";
}

我正在尝试执行此操作$ar[]=$row;但没有任何显示!

$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {

ar[] = $row;
echo "<br/>";
}
for($i = 0, $i<12, $i++) {
echo ar['field'][0];
}

输出中不显示任何内容

最佳答案

您可以执行以下方法将列名存储到数组中。阅读每行后的评论

对于 PDO

$rs = $cid->query('SELECT * FROM commissions');// your query
for ($i = 0; $i < $rs->field_count(); $i++) {/// count nu of column
$col = $rs->fetch_fields()($i);//Returns metadata for a column in a result set
$columns[] = $col['name'];// get name from metedata
}
print_r($columns);

对于 mysqli

$rs = $cid->query('SELECT * FROM commissions');// your query

$col = $rs->fetch_fields();//Returns metadata for a column in a result set
foreach ($col as $val) {
$columns[]=$val->name;
}
print_r($columns);

关于php - 如何将表字段(列)名称存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com