2

I am trying to display all table names in my database. This is my code:

<?php
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'nucleus');
$db=new mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
$query = $db->prepare('SHOW TABLES FROM `nucleus`');
$query->execute();
while($rows = $query->fetch()){
     echo " $rows";
}
?>

All it is displaying 1 for every entry, I don't know why. Any solution for this?

3
  • Please edit to improve formatting (the "quote" format is wrong for the body of your question and the readability of your code could improve by being more compact). Commented Aug 2, 2017 at 17:29
  • Thats because you did not bind your results. look at the manual example php.net/manual/en/mysqli-stmt.fetch.php Commented Aug 2, 2017 at 17:29
  • yeah I did,I got the answer Commented Aug 2, 2017 at 17:30

6 Answers 6

8

To get the name of all tables use:

SELECT table_name FROM information_schema.tables;

To get the name of the tables from a specific database use:

SELECT table_name FROM information_schema.tables where table_schema='<your_database_name>';

For more details see: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html

Sign up to request clarification or add additional context in comments.

2 Comments

I got an easier version :) ,Thanks anyways
This is answering a different question.
1

So, anyone looking for possible answer, here it is:

<?php
$query = $db->prepare('SHOW TABLES FROM `databasename`');
$query->execute();
$query->bind_result($tables);
while( $query->fetch()){
         echo " $tables";
}
?>

1 Comment

The answer is that $rows is an array, not a string. echo $rows['TABLES'];... or output $rows and see what the index is.
1
SHOW TABLE STATUS

if the connection already made, no need to mention the db name

Comments

0

All it is displaying 1 for every entry, I don't know why

fetch() returns a boolean, therefore your result of 1. It will return true every time it finds a row.

You need to use bind_result() to bind the result to a variable that you can reference for each row.

$stmt->prepare("SELECT table_name FROM information_schema.tables where table_schema=?")
$stmt->bind_param('s', 'YOUR_DB_NAME');
$stmt->execute();
$stmt->bind_result($table);
$stmt->store_result();
while($stmt->fetch()) {
    echo $table;
}
$stmt->close();

Comments

0

I did a complete solution working, there are two ways to get SQL results, I preferred precedence to get results, $ROW will receive array data to show:

    <?php 

        $connection=conectadb();
        $sql = 'SHOW TABLES FROM pmsai';

        $result = $connection->query($sql);

        if (!$result) {
            echo "DB Error, could not list tables\n";
            echo 'MySQL Error: ' . mysqli_error();
            exit;
        }

        while ($row = $result->fetch_array()) {
            echo "<p>Table: {$row[0]}</p>";
        }

        mysqli_close($connection);          

    ?>

1 Comment

While this code snippet may solve the question, including an explanation helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

You can do this in 2 ways:

  1. Select the DB and then see the list of Tables
    # Select a DB
    SELECT db_name
    
    # See Selected DB
    SELECT DATABASE();
    
    # See list of tables
    SHOW TABLES;

  1. See table list without switching selected Table

    SHOW TABLES FROM database_name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.