doushi1964 2018-12-10 16:43
浏览 60
已采纳

尝试通过从所选表中获取列名来构建我的查询

I'm working automatically and dynamically generating SQL queries for inserting CSV data into a selected database. Now I have a list of 10 different databases. Now I'm curious if it is possible to build a part of my query dynamically (the table names) by fetching the column name from the database?

This is the code I have right now but it doesn't quite work:

function getTableDetails($table_name) {
    global $con, $user;

    $describeTable = mysqli_query($con, "DESCRIBE " . $user . "." . $table_name);

    $fields = [];
    while($show = mysqli_fetch_fields($describeTable)) {
        $fields['column_name'][] = $show['COLUMN_NAME'];
        $fields['column_length'][] = $show['CHARACTER_MAXIMUM_LENGTH'];
        $fields['column_type'][] = $show['COLUMN_TYPE'];
    }

    return $fields;
}

How I try to fetch them

$table = getTableDetails($settings_type);
foreach ($table['column_name'] as $columnName) {
    print_r($columnName);
}
  • 写回答

1条回答 默认 最新

  • douhuang75397 2018-12-10 16:58
    关注

    I've changed the function slightly to pass in the fields which you access using global (as this isn't recommended). So you will have to alter the call to getTableDetails().

    mysqli_fetch_fields() is used to return the fields which are part of the result set, as this is from a describe, you were fetching the fields which were the return values of the describe rather than the fields in the table. Instead you need to use mysqli_fetch_assoc() which returns the rows of data from the statement.

    The other thing to always check is if you have problems with fetching data is to use print_r() to check what is being returned.

    I've also indexed the data by the column name as well, this can be useful sometimes, but you could also just use $fields[] = [....

    As the field length isn't part of the field set being returned, I've added code which will extract it from the data type, so int(11) has the value 11 extracted from between the brackets using preg_match().

    function getTableDetails( $con, $user, $table_name) {
        $describeTable = mysqli_query($con, "DESCRIBE " . $user . "." . $table_name);
    
        $fields = [];
        while($show = mysqli_fetch_assoc($describeTable)) {
            $columnName = $show['Field'];
            // Extract length from field type (if any)
            preg_match('#\((.*?)\)#', $show['Type'], $match);
            $fields[$columnName] = ['column_name' => $show['Field'],
                'column_length' => $match[1]??0,
                'column_type' => $show['Type']];        
        }
    
        return $fields;
    }
    
    $table = getTableDetails( $con, $user, "articles");
    foreach ($table as $columnName) {
        print_r($columnName);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里