如何通过php mysql获取所有表字段?

时间:2015-10-12 14:01:56

标签: php mysql

$link = mysqli_connect("localhost", "root", "",$database);
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SHOW COLUMNS FROM area";
$query = mysqli_query($link,$sql);
echo '<pre>';    print_r(mysqli_fetch_array($query)); 

以上是我的代码,它只返回id(第一个字段)

2 个答案:

答案 0 :(得分:0)

编辑:我的不好,抱歉。

MySQLi examples on PHP.net所示,您希望了解结果;你的代码看起来像:

$link = new mysqli("localhost", "root", "", $database);
$cursor = $link->query('SHOW COLUMNS FROM area;');
while ($row = $cursor->fetch_assoc())
{
  print_r($row);
}

答案 1 :(得分:0)

你必须像这样获取所有行

$link = mysqli_connect("localhost", "root", "",$database);
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SHOW COLUMNS FROM area";
$query = mysqli_query($link,$sql);
echo '<pre>';
while ($res = mysqli_fetch_array($query)){
    print_r($res); 
}
echo '</pre>';