HTML HTML5 PHP Mysql Linux 缓存技术 工具 资讯 读书 其他
当前位置: 资讯首页 » 全文内容

获取mysql数据库中所有字段

发布于: 2018-03-30 05:53:15 )
function query($sql, $dbname = '') {
    $dbConf = array(
        'host'     => '127.0.0.1',
        'user'     => 'user',
        'password' => 'pwd',
        'port'     => 3306,
    );
    $ret         = array();
    $db          = mysqli_connect($dbConf['host'], $dbConf['password'], $dbConf['password'], $dbname, $dbConf['port']);
    $retry_times = 20;
    while (mysqli_connect_errno()) {
        sleep(1);
        $db          = mysqli_connect($dbConf['host'], $dbConf['password'], $dbConf['password'], $dbname, $dbConf['port']);
        if ($retry_times-- <= 0) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit(1);
        }
    }
    if ($result = mysqli_query($db, $sql)) {
        while ($row = mysqli_fetch_assoc($result)) {
            $ret[] = $row;
        }
    }

    return $ret;
}

$dbName = 'database_name';
$tableName = 'talbe_name';

$sql    = "select column_name from information_schema.columns where table_name= '" . $tableName . "' and TABLE_SCHEMA='" . $dbName . "'";
$res    = query($sql,$dbName);
foreach ($res as $item) {
    $fieldsArr[] = $item['column_name'];
}
echo PHP_EOL;
$fields =  '`'.implode('`,`', $fieldsArr).'`';
echo "fields : ". fields;
echo PHP_EOL;
exit;

To Top