php - 从数据库中获取列名

标签 php mysql sql

在这个程序中,我从数据库中获取了列名,但在尝试将其保存在数组中时,它会将最后一个列名存储在数组中。如何将 eash 列名存储在数组中???

$sql="SHOW COLUMNS from itasd2015sem1semester";
if(($result=$conn->query($sql))==true){
    while($row=$result->fetch_array()){
        for($i=0;$i<11;$i++){
            $sub[$i]=$row[0]."\n";
        }
    }
}

for($i=0;$i<sizeof($sub);$i++){
    echo $sub[$i];
}

它只打印最后一列的名称??

$sql="SHOW COLUMNS from itasd2015sem1semester";
if(($result=$conn->query($sql))==true){
    while($row=$result->fetch_array()){
        for($i=0;$i<11;$i++){
            $sub[$i]=$row[$i]."\n";
        }
    }
}

for($i=0;$i<sizeof($sub);$i++){
    echo $sub[$i];
}

它显示一条通知..并且它还单独打印最后一列。

Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 6 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 7 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 8 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 9 in C:\xampp\htdocs\new\test1.php on line 21
Notice: Undefined offset: 10 in C:\xampp\htdocs\new\test1.php on line 21

最佳答案

试试这个:

$sql = "SHOW COLUMNS from itasd2015sem1semester";
if (($result = $conn->query($sql)) == true) {
    while ($row = $result->fetch_array()) {
        $sub[] = $row[0];
    }
}
for ($i = 0; $i < sizeof($sub); $i++) {
    echo $sub[$i];
}

关于php - 从数据库中获取列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040079/

相关文章:

sql - 如何计算作为不同值总和的运行总计

mysql - 在 MySQL 中以 root 身份登录不需要密码

php - PHP 中用 ' 代替撇号 '

php - 使用 jquery.confirm 提交表单

php - 通过将 CSS 文件合并到一个文件中来加快网站速度 - Laravel

mysql - 在第一个查询中使用所选列并在第二个查询中使用 IN 子句

MySql 多对多仅选择连接行处的固定行

c# - 在插入中使用来自另一个表的值

php - 连接失败: Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server - AJAX and AWS

mysql - 如何最优地构建此表,每个用户的技能基于另一个表