CakeFest 2024: The Official CakePHP Conference

mysql_tablename

(PHP 4, PHP 5)

mysql_tablename取得表名

警告

本扩展自 PHP 5.5.0 起已废弃,并在自 PHP 7.0.0 开始被移除。应使用 MySQLiPDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南来获取更多信息。用以替代本函数的有:

  • SQL Query: SHOW TABLES

说明

mysql_tablename(resource $result, int $i): string|false

result 中取得表名。

此函数已废弃,推荐使用 mysql_query() 函数来执行 SQL SHOW TABLES [FROM db_name] [LIKE 'pattern'] 替代。

参数

result

接受 mysql_list_tables() 返回的结果指针以及一个整数索引作为参数并返回表名。

i

The integer index (row/table number)

返回值

成功时返回表名 或者在失败时返回 false

Use the mysql_tablename() function to traverse this result pointer, or any function for result tables, such as mysql_fetch_array().

示例

示例 #1 mysql_tablename() example

<?php
mysql_connect
("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("mydb");
$num_rows = mysql_num_rows($result);
for (
$i = 0; $i < $num_rows; $i++) {
echo
"Table: ", mysql_tablename($result, $i), "\n";
}

mysql_free_result($result);
?>

注释

注意:

The mysql_num_rows() function may be used to determine the number of tables in the result pointer.

参见

add a note

User Contributed Notes 2 notes

up
5
Haseldow
19 years ago
Another way to check if a table exists:

if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists";
else echo "Table does not exist";
up
-23
pl at thinkmetrics dot com
20 years ago
A simple function to check for the existance of a table:

function TableExists($tablename, $db) {

// Get a list of tables contained within the database.
$result = mysql_list_tables($db);
$rcount = mysql_num_rows($result);

// Check each in list for a match.
for ($i=0;$i<$rcount;$i++) {
if (mysql_tablename($result, $i)==$tablename) return true;
}
return false;
}
To Top