gpt4 book ai didi

Mysql pdo 列类型、名称和值

转载 作者:行者123 更新时间:2023-11-29 16:00:12 25 4
gpt4 key购买 nike

我正在使用 php 7.2、mysql pdo 并尝试获取列类型、名称和值。我一直在使用它,它对列名和值很有帮助,但现在我想动态读取类型(varchar、text、date 等...)

$sql = "SELECT * FROM myTable WHERE id = :id limit 1";

$st = $db->prepare($sql);
$st->execute([":id" => $id]);
$row = $st->fetch(PDO::FETCH_ASSOC);

if (count($row) >= 1) {
foreach ($row as $column => $value) {
$$column = $value;
}

}

最佳答案

尝试执行以下查询:

SHOW COLUMNS FROM myTable;

例如:

mysql> SHOW COLUMNS FROM price;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| price_id | int(11) | NO | PRI | NULL | auto_increment |
| effective_date | date | NO | | NULL | |
| min_quantity | int(11) | NO | | NULL | |
| price | decimal(7,0) | NO | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

字段将为您提供列名称键入列类型,Null 将为“NO”或“YES”,具体取决于列是否可以为空,Key如果该列是主键的一部分,则将为“PRI”。请阅读手册。

关于Mysql pdo 列类型、名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56243386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com