1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mysql旧pearと利用しない方法でテーブルフィールド名取得

Last updated at Posted at 2022-04-02

pearによるDBのtablesinfoにてフィールド名取得例。

require_once 'DB.php';
$dsn = "mysqli://root:password@localhost/db名";
$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
);
$db = DB::connect($dsn);
if (DB::isError($db)) {
   die($db->getMessage());
}
$db->query('SET NAMES utf8');
$info = $db->tableInfo('テーブル名',0);
//print_r($info);
$i=0;
foreach($info as $key => $value){
//echo $info[$i][len]."<BR>";
echo $info[$key][name]."<BR>";
}

pearを利用しないフィールド名取得。

$mysqli = new mysqli('127.0.0.1', 'root', 'パスワード', 'db名');
if ($mysqli->connect_error) {
    echo $mysqli->connect_error;
    exit();
} else {
    $mysqli->set_charset("utf8");
	echo "connect ok!!";
}
$query = "SELECT * from テーブル名 order by pet_id";
if ($result = $mysqli->query($query)) {
echo "result ok!!";
        /* すべてのカラムのフィールド情報を取得します */
        $finfo = $result->fetch_fields();
//print_r($finfo);

        foreach ($finfo as $val) {
		//print_r($val);
		echo $val->name."<br>";
	}

}else{
	echo "not result";

}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?