gpt4 book ai didi

php - 枚举 mysql 查询中使用的表?

转载 作者:行者123 更新时间:2023-11-29 01:34:25 28 4
gpt4 key购买 nike

有没有办法枚举mysql查询中使用的表?

假设我有查询:

SELECT * FROM   db_people.people_facts pf
INNER JOIN db_system.connections sm ON sm.source_id = pf.object_id
INNER JOIN db_people.people p ON sm.target_id = p.object_id
ORDER BY pf.object_id DESC

我想要返回数组:

$tables = array(
[0] => 'db_people.people_facts',
[1] => 'db_system.connections',
[2] => 'db_people.people',
);

最佳答案

是的,您可以获得有关作为查询结果一部分的表和列的信息。这称为结果集元数据

MySQL 结果集元数据的唯一 PHP 解决方案是使用 MySQLi 扩展和 mysqli_stmt::result_metadata()功能。

$stmt = $mysqli->prepare("SELECT * FROM   db_people.people_facts pf
INNER JOIN db_system.connections sm ON sm.source_id = pf.object_id
INNER JOIN db_people.people p ON sm.target_id = p.object_id
ORDER BY pf.object_id DESC");

$meta = $stmt->result_metadata();

$field1 = $meta->fetch_field();

echo "Table for field " . $field1->name . " is " . $field1->table . "\n";

您必须自己构建查询中使用的不同表的数组,方法是遍历字段。

关于php - 枚举 mysql 查询中使用的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/740316/

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