从数据库中获取列名称

时间:2014-04-13 07:36:41

标签: php mysql sql

我这个程序我从数据库中获取列名但是在尝试将其保存在数组中时,它将最后一个列名存储在array.how中以将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

2 个答案:

答案 0 :(得分:3)

试试这个:

$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];
}

答案 1 :(得分:0)

当我查询我的一些名为administrator的表时:

SHOW COLUMNS FROM `administrator`

它返回

enter image description here

以下代码应该可以正常工作

$sql = "SHOW COLUMNS FROM `itasd2015sem1semester`";
$sub = array();
if( ($result = $conn->query($sql)) !== FALSE ) {
    while( $row = $result->fetch_array() ) {
        $sub[] = $row[0]; // get first column ('Field')
    }
}

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