PHP - Popular dropdown using a database table name

Asked

Viewed 243 times

0

The title is already self-explanatory. I’ve tried everything, but I feel like everything I’ve done is completely wrong, haha. If anyone can help me, I need to dropdown where the values to be selected will be table names from my database. Thank you.

[EDIT] The easiest way I see is to pull all tables through the function mysqli_list_tables and then I would use a while, or I would print this in the dropdown. The point is that even this simple function below is not working. I tried to leave only the fundamental part (the function of listing tables) for testing purposes and even then, nothing works. (Even, in my index file, after calling the function dropdownAnos() nothing else works).

function dropdownAnos () {
  $host = "localhost";
  $user = "root";
  $pass = "root";
  $banco = "teste";

  mysqli_connect($host, $user, $pass);

  $tabelas = mysqli_list_tables($banco);

  print_r($tabelas);
}

I’ve changed this code over and over again, I’ve tried every possible way and this is the simplest version I could get. Even so, it doesn’t work. (I’m glad I saved it on github, otherwise I would have lost everything haha).

[EDIT FINAL] I did it, guys. I’ll post the answer in case someone falls for this topic:

function dropdownAnos () {
  $str = "";
  $db = new mysqli('localhost', 'root', 'root', 'teste');

  $teste = $db->query('SHOW TABLES');

  while ($t = $teste->fetch_array()) {
    $str = $str .  "<option>" . $t[0] .  "</option><br>";
  }

  echo $str;
}

Thank you to everyone who helped.

  • 1

    @Michael please post what you did!

  • 1

    Make a tour by stackoverflow, learn a little about the rules and good practices. Then edit your question and enter the code of what you’ve tried...

  • 1

    The title is not self-explanatory in itself, we need the code in order to help you. It’s hard to know what you need if you don’t show us what you’re trying to do.

  • This function does not exist in the extension MySQLi only in the old.

  • @rray then the way would be to make a query like "show Tables from $bank"? I’ve tried and it didn’t work

  • And you already tried to consult select * from information_schema.tables - Reference

  • @Marcogiovanni already yes, but I must have done something wrong. I managed to do using "show Tables", posted the answer in the question, thank you.

  • @Michaelsampietro left the tab open here and had not updated hehe

Show 3 more comments

1 answer

2


There is no function matching mysql_list_tables() for Mysqli extension, the way is to make an appointment in the same hand.

You can list all tables in the current database, by information_schema or your shortcuts (SHOW TABLES).

function listarTabelas(){
   $db = new mysqli('localhost', 'usuario', 'senha', 'database');
   $sql = 'show tables';
   $result = $db->query($sql);
   return $result->fetch_all();
}

function criarOptions($itens){
    $options = '';
    foreach($itens as $item){
       $options .= sprintf('<option>%s</option>', $item); 
    }
    return $options;
}

The call can go like this:

<?php $options = criarOptions(listarTabelas()); ?>
<select>
   <?php echo $options; ?>
</select>

Recommended reading:

How to get the name of all tables in a Mysql database

  • Thank you, I managed to do it another way:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.