请求在mysql中显示列名

时间:2019-06-20 09:38:36

标签: php mysql

我使用PHP,MySQL开发网站,并希望列出表转换的列名。

我已经编写了这段代码,但无法正常工作。

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = translate;

此代码在MySQL中有效,但返回的列名称为double,而在PHP中则无效。

这是我的php代码:

try{
    $bdd -> new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'root',       '');
  }catch (Exception $e)
  {
      die('Erreur: ' . $e->getMessage());
  }

  $req -> $bdd -> prepare('SELECT COLUMN_NAME 
                     FROM INFORMATION_SCHEMA.COLUMNS 
                     WHERE TABLE_NAME = translate;');
  $req->execute();

1 个答案:

答案 0 :(得分:0)

尝试一下:-

<?php

$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'dbname';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!$conn) {
    die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';

$sql = 'SHOW COLUMNS FROM tablename';
$result = mysqli_query($conn, $sql);



if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row["Field"] . "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($conn);
?>

我的桌子是:-

enter image description here

输出为:-

enter image description here