获取MySQL表的列表并为每个表运行一个代码 [英] Get list of MySQL tables and run a code for every table
本文介绍了获取MySQL表的列表并为每个表运行一个代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该怎么做?
SHOW TABLES :
$ pdo = new PDO('mysql:dbname = mydb','myuser','mypass');
$ stmt = $ pdo-> query('SHOW TABLES;');
$ b $ if($ stmt-> rowCount()> 0){
$ tables = $ stmt-> fetchAll(PDO :: FETCH_NUM);
foreach($ tables as $ table){
$ table_name = $ table [0];
//做某事
}
}
I have a number of tables in one mysql database. I need to store the table name in a variable and run some code on each table name.
How can I do that?
解决方案
Use SHOW TABLES
:
$pdo = new PDO('mysql:dbname=mydb', 'myuser', 'mypass');
$stmt = $pdo->query('SHOW TABLES;');
if ($stmt->rowCount() > 0) {
$tables = $stmt->fetchAll(PDO::FETCH_NUM);
foreach ($tables as $table) {
$table_name = $table[0];
// do something
}
}
这篇关于获取MySQL表的列表并为每个表运行一个代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文