1

So, I have the following code using PDO method to get all the tables from my DB.
But I need to put it inside a dropdown menu (the ideia it's to display all the records of the selected table so the user can export that records from the table that he selected to an .csv file)
Can anyone help me please?

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $pdo->prepare('Show Tables from filecleaner');
$query->execute();

while($rows = $query->fetch(PDO::FETCH_ASSOC)){
     var_dump($rows);
}
12
  • but this will show u only tables Show Tables from filecleaner all tables. ??? Commented Mar 18, 2019 at 14:57
  • @devpro yes, when I var_dump it shows me all the tables. Commented Mar 18, 2019 at 15:01
  • @formador3 show results of var_dump Commented Mar 18, 2019 at 15:02
  • 1
    and what exactly is your problem? generating a select menu? Commented Mar 18, 2019 at 15:04
  • 1
    @formador3 if you can provide the results of your var_dump we could easy help u Commented Mar 18, 2019 at 15:17

1 Answer 1

1

You can display all tables name from filecleaner in a select box like

$allTables = array(); // initialize your array
while($rows = $query->fetch(PDO::FETCH_BOTH)){ 
    $allTables[] = $rows[0]; // store all table name in an array. $rows[0] index will give you table name from filecleaner
}

then, display like:

<select>
    <option>Select Table</option>
    <?php
    foreach ($allTables as $key => $value) {
    ?>
        <option><?=$value?></option>
    <?php
    }
    ?>
</select>

Edit:

i think you are not getting $rows[0] due to FETCH_ASSOC, you need to use FETCH_BOTH here to get 0 indexed column number.

Issue in your code is: FETCH_ASSOC returns an array indexed by column name, and you are trying to display with 0 indexed column number.

Reference

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

3 Comments

It displays an empty select and the error Undefined offset: 0 x.x
@formador3: glad to help u, actually i run a test script with my database, and get the idea, what actually happening. :)
I should take some time to really learn about all the syntax and debugging hahaha, Thanks ! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.