marry jones
marry jones

Reputation: 17

When selecting any database it only displays the number of tables not the names of all tables of the selected database

<?php
    session_start();
    include 'dbconnect.php';
    $q=$_GET['q'];
    echo $field= "Tables_in_".$q;
    $sql = "SHOW TABLES FROM $q";
    $result= mysqli_query($conn,$sql);
    $new= array();
    $i=0;
    while ($row=mysqli_fetch_row($result)){
        echo $row['Tables_in_mysql'];
        $new[$i]=$row;
        $i++;
    }
    echo $num=count($new);
?>

This is my code please help me in printing all the names of the tables of the selected database in php I have shown all the names of the database by show database command and it is displaying in a dropdown and while selecting particular database it only displays the number of the tables in it but i want to display names of tables

Upvotes: 2

Views: 40

Answers (1)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

Please see if any error is there in your mysqli call.

Here is the working code.

$conn = new mysqli("localhost", "root", "");
$dbname = 'db_name';

$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);

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

while ($row = mysqli_fetch_row($result)) {
    echo "Table: {$row[0]}\n";
}

Upvotes: 1

Related Questions