mysql数据库遍历所有表 php_php mysql_list_tables获取数据库中所有表

本文介绍了如何在PHP中使用`SHOW TABLES` SQL命令替代已废弃的`mysql_list_tables()`函数来遍历MySQL数据库中的所有表。通过示例代码展示了正确的实现方式。
摘要由CSDN通过智能技术生成

mysql_list_tables获取数据库中所有表

mysql_list_tables介绍

resource mysql_list_tables ( string database [, resource link_identifier] )

mysql_list_tables() 接受一个数据库名并返回和 mysql_query() 函数很相似的一个结果指针。用 mysql_tablename() 函数来遍历此结果指针,或者任何使用结果表的函数,例如 mysql_fetch_array()。

database 参数是需要被取得其中的的表名的数据库名。如果失败 mysql_list_tables() 返回 FALSE。

为向下兼容仍然可以使用本函数的别名 mysql_listtables(),但反对这样做。

注: 该函数已经被删除了,请不要再使用该函数。您可以用命令 SHOW TABLES FROM DATABASE 来实现该函数的功能。

mysql_list_tables() 实例

$dbname = 'mysql_dbname';

if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {

print 'Could not connect to mysql';

exit;

}

$result = mysql_list_tables($dbname);

if (!$result) {

print "DB Error, could not list tables\n";

print 'MySQL Error: ' . mysql_error();

exit;

}

while ($row = mysql_fetch_row($result)) {

print "Table: $row[0]\n";

}

mysql_free_result($result);

?>

上面已经说过,mysql_list_tables()函数已经被删除了,所以上面实例会报错:

Deprecated: Function mysql_list_tables() is deprecated

正确的方法是使用mysql_query("SHOW TABLES FROM $database")代替mysql_list_tables()函数。

mysql_connect("127.0.0.1","root","password");

$tables = mysql_query("SHOW TABLES FROM information_schema");

while (list($table) = mysql_fetch_row($tables)) {

echo "$table
";

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值