I have looked around and still can't find how to list all my tables in a database. is it po开发者_如何学编程ssible with MySQLi?
Thanks.
There are many ways.
SHOW TABLES
Is the most simple SQL statement for doing that. You can also take a look at INFORMATION_SCHEMA.TABLES
if you want to have more details or do some filtering or such.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'your_database';
Using PHP 5.5 or later, a simple solution is using PHP's built-in array_column() function.
$link = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
$listdbtables = array_column($link->query('SHOW TABLES')->fetch_all(),0);
I'd try something like:
function get_tables()
{
$tableList = array();
$res = mysqli_query($this->conn,"SHOW TABLES");
while($cRow = mysqli_fetch_array($res))
{
$tableList[] = $cRow[0];
}
return $tableList;
}
$link = mysqli_connect("localhost", "domain_root", "##pwd", "domain_DBname");
$query = mysqli_query($link, "SHOW TABLES IN domain_DBname");
$numrows = mysqli_num_rows($query);
echo "<b>Amount of tables: ".$numrows." and their names:</b>";
while ($row = mysqli_fetch_array($query)) {
echo $row[0]." ";
}
here is little example
class database {
public $connection;
function __construct() {
$this->connection = mysqli_connect(DBHOST,
DBUSER,
DBPASS,
DBNAME) or
die('Database Connection Error: '.mysqli_connect_error());
}
public function close_database() {
return mysqli_close($this->connection);
}
public function query($query) {
$query = mysqli_query($this->connection ,$query) or die($this->show_errors('Query Execution Error: '.mysqli_error($this->connection),'E'));
return $query;
}
public function fetch_assoc($query) {
$query = mysqli_fetch_assoc($query);
return $query;
}
}
$db = new database();
$query = $db->query("SHOW TABLES FROM DATABASENAME");
$db->fetch_assoc($query);
精彩评论