2024-09-26 05:21:39 | 我爱编程网
connect_error) 不返回错误信息?" src="/draw/thumb/42079/10c819c5a7590cfb1c04e6f1192b6fb3.png">
connect_error) 不返回错误信息?" src="/draw/image/42079/6d02b6ff157b3d272eeebb082d459464.png">
connect_error) 不返回错误信息?" src="/draw/image/42079/0b8a6aeb2b2d1a8c55e92634a9cfdb72.png">
我爱编程网(https://www.52biancheng.com)小编还为大家带来用php调用mysql数据表中指定id下所有的子子孙孙数据;的相关内容。
写一个递归函数即可。
$host = '';我爱编程网以上就是我爱编程网小编整理的内容,想要了解更多相关资讯内容敬请关注我爱编程网。更多相关文章关注我爱编程网:www.52biancheng.com
$dbname = '';
$dsn = "mysql:host=$host,dbname=$dbname";
try {
$dbh = new PDO($dsn, 'root', 'root');
} catch (PDOException $e) {
die($e -> getMessage());
}
$child = function ($id) use($dbh) {
static $final = [];
$sql = "SELECT * FROM tablename WHERE id = $id"; // 找出需要查询的数据
$data = $dbh -> query($sql) -> fetch(PDO::FETCH_ASSOC);
$final[] = $data;
$sql = "SELECT * FROM tablename WHERE pid = " . $data['id']; // 找出当前数据的子孙数据
$datas = $dbh -> query($sql) -> fetchall(PDO::FETCH_ACCOS);
foreach ($datas as $value) {
child($value['id']); // 无限递归
}
return $final;
}
$id = 1; // 需要查询的数据ID
$final = $child($id);
var_dump($final);
2025-02-01 20:24:39
2024-01-05 14:11:24
2025-02-12 03:21:37
2025-02-10 15:19:48
2025-01-28 17:58:32
2024-11-22 05:08:01