PHP獲取MySQL資料庫裡所有表的實現程式碼
如何獲取某個MySQL資料庫中所有表的`PHP程式碼如下,需要的朋友可以參考下。跟隨小編去看看啊!
程式碼如下:
function list_tables($database)
{
$rs = mysql_list_tables($database);
$tables = array();
while ($row = mysql_fetch_row($rs)) {
$tables[] = $row[0];
}
mysql_free_result($rs);
return $tables;
}
但由於mysql_list_tables方法已經過時,執行以上程式時會給出方法過時的提示資訊,如下:
複製程式碼 程式碼如下:
Deprecated: Function mysql_list_tables() is deprecated in … on line xxx
一個處理辦法是在中設定error_reporting,不顯示方法過時提示資訊
複製程式碼 程式碼如下:
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED
另一個方法是使用PHP官方推薦的替代做法:
複製程式碼 程式碼如下:
function list_tables($database)
{
$rs = mysql_query("SHOW TABLES FROM $database");
$tables = array();
while ($row = mysql_fetch_row($rs)) {
$tables[] = $row[0];
}
mysql_free_result($rs);
return $tables;
}