yandex
loader

please wait

  • Pooja Bhardwaj Mar-15-2019 07:00:11 AM ( 1 week ago )

    I've just started learning php.

    I am trying to get a list of tables from my database. what I have tried -

    php   
        $connection = new mysqli("localhost", "root", "");
        $sql = "SHOW TABLES FROM MyDatabase";
        $result = $connection->query($sql);
    
        while ($row = $result->fetch_assoc()) {
           echo "Table: {$row[0]}\n"; //error here
        }
    ?>

    But it does not show tables and returns - Notice: Undefined offset: 0. Any help?

  • Vanshika Bhatt Mar-15-2019 07:01:54 AM ( 1 week ago )

    Try fetch_array() inplace of fetch_assoc()

    fetch_array() returns one array with both numeric keys, and associative strings (column names), so here you can either use $row['column_name'] or $row[0]

    Where as fetch_assoc() will return string indexed key array and no numeric array so you won't have an option here of using numeric keys like $row[0].

    Change just below code -

    php   
        $connection = new mysqli("localhost", "root", "");
        $sql = "SHOW TABLES FROM MyDatabase";
        $result = $connection->query($sql);
    
        while ($row = $result->fetch_array()) {
           echo "Table: {$row[0]}\n";
        }
    ?>

Please login

Similar Discussion

Recommended For You