mysql_tablename() function for PHP

What is mysql_tablename() function in PHP?
How does mysql_tablename() works?

Explanation

Mysql function mysql_tablename() can returns table name of field.
Syntax
string mysql_tablename ( resource result, int row)

Retrieves the table name from result.
mysql_tablename() takes a result pointer returned by the mysql_list_tables() function as well as an integer index and returns the name of a table. The mysql_num_rows() function may be used to determine the number of tables in the result pointer. Use the mysql_tablename() function to traverse this result pointer and print the table name
Example:

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//list the tables in the database
$result = mysql_list_tables("my_database");
//find the number of tables in database
$num_rows = mysql_num_rows($result);
//list the table name
for ($i = 0; $i < $num_rows; $i++) {
echo "Table: ", mysql_tablename($result, $i), "n";
}
mysql_free_result($result);
?>

RESULT:
Table: my_table1
Table: my_table2
Table: my_table3
Table: my_table4
Table: my_table5

Instead of mysql_tablename() function mysql_fetch_array() can be used to traverse this result pointer and print the table name.
Example

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//list the tables in the database
$result = mysql_list_tables("my_database");
//query result is passed to mysql_fetch_array to fetch each row
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
//access result array with number index
printf ("Table: %s", $row[0]);
} ?>

RESULT:
Table: my_table1
Table: my_table2
Table: my_table3
Table: my_table4
Table: my_table5

See also: mysql_list_tables().

PHP Topics


Ask Questions

Ask Question