Hi friends!
With this script I can't get (and print) the names of the tables:

$sql_tables = mysqli_query($db, "SHOW TABLES FROM `MyDatabase WHERE `Tables_in_MyDatabase` IN ('tableone', 'tabletwo', 'tablethree')");
$total_tables = mysqli_num_rows($sql_tables);
$i = 0;

while ($i < $total_tables) {
	$thetable = mysqli_fetch_field_direct($sql_tables, $i)->table;
	echo $thetable;
}

I get the following error:

Warning: mysqli_fetch_field_direct(): Field offset is invalid for resultset in ....
Notice: Trying to get property of non-object in...

How can I resolve?
Thanks a lot 😉

    The MySQLi extension does not appear to have a function named mysqli_fetch_field_direct, at least not one that is listed in the PHP manual. Did you define this function yourself?

      I get it from here...
      Anyway, there is a way to retrieve the table name in my code?

        Ah, my bad: seems like the PHP manual's search functionality for functions that are aliases is broken.

        I am not sure if mysqli_fetch_field_direct is the right tool for the job, but I do not have a MySQL database available to find out for myself, so I shall make do with your attempt. For starters, I note that your SQL statement has a typo error: "MyDatabase" should have been "MyDatabase". You should therefore fix it and check for further errors, e.g.,
        [code=php]$sql = "SHOW TABLES FROM
        MyDatabaseWHERETables_in_MyDatabase` IN ('tableone', 'tabletwo', 'tablethree')";
        $result = mysqli_query($db, $sql) or die(mysqli_error($db));
        $total_tables = mysqli_num_rows($result);
        for ($i = 0; $i < $total_tables; ++$i) {
        echo mysqli_fetch_field_direct($result, 0)->table;
        }[/code]
        I have chosen to change $i to 0 in the mysqli_fetch_field_direct call since SHOW TABLES should only produce a result set with one column. But here's where mysqli_fetch_field_direct does not seem like the right tool for the job: calling it for each row does not make sense, since it is concerned with a field of the result set irrespective of row (i.e., a column). Perhaps you should try mysqli_fetch_row in a loop instead.

          trying your example, i see printed only "TABLE_NAMES".

          But if I execute your own query via PhpMyAdmin it works fine and returns the table names

            joane1 wrote:

            But if I execute your own query via PhpMyAdmin it works fine and returns the table names

            It is the query that you wrote, with a one character fix. Since it works for you when you directly execute the query, I suggest trying my suggestion of [man]mysqli_fetch_row[/man] instead of mysqli_fetch_field_direct.

              joane1;11044825 wrote:
              $sql_tables = mysqli_query($db, "SHOW TABLES FROM `MyDatabase WHERE `Tables_in_MyDatabase` IN ('tableone', 'tabletwo', 'tablethree')");
              $total_tables = mysqli_num_rows($sql_tables);
              $i = 0;
              
              while ($i < $total_tables) {
              	$thetable = mysqli_fetch_field_direct($sql_tables, $i)->table;
              	echo $thetable;
              }

              You should ALWAYS check to see if a query results in an error or not. You should see from the documentation on mysqli_query that it will return the following:

              The Friendly Manual wrote:

              Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

              If your query is broken (which laserlight pointed out) then $stl_tables will be FALSE because the query failed.

                Write a Reply...