【问题标题】:Get a column name from database从数据库中获取列名
【发布时间】:2014-05-27 05:33:31
【问题描述】:

我这个程序我从数据库中获取列名,但是当尝试将它保存在一个数组中时,它将最后一个列名存储在一个数组中。如何将 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];
}

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

注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 6 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 7 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 8 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 9 注意:未定义的偏移量:第 21 行 C:\xampp\htdocs\new\test1.php 中的 10

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 每列打印 11 次
    • 我不知道您的 $result 是如何工作的,但请尝试在您的 while 循环中添加以下代码: if (in_array($row[0], $sub)) { continue; }
    • 我必须在哪里添加这个??
    • 在你的 while 循环中和之前 '$sub[] = $row[0];'
    【解决方案2】:

    当我查询我的一些名为管理员的表时:

    SHOW COLUMNS FROM `administrator`
    

    返回

    所以下面的代码应该可以工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多