2

How would I be able to make a list of tables and traverse them in php?

I tried to figure out how the mysqli_query would output, but I keep getting an error.

$link = mysqli_connect($host,$user,$pass);
mysqli_select_db($link,$name);
$result = mysqli_query($link, "SHOW TABLES");
echo $result; 

Essentially I want to be able to place them in an array and traverse the array going through each single table and do a quick query on them.

Thanks for the help.

edit: title

7
  • 1
    What is this error you are getting exactly? Commented Sep 10, 2012 at 21:47
  • "Catchable fatal error: Object of class mysqli_result could not be converted to string" and this is pertaining to the line "echo $result;" Commented Sep 10, 2012 at 21:48
  • 2
    don't do show tables. use the INFORMATION_SCHEMA db. that's what it's there for. Commented Sep 10, 2012 at 21:50
  • First you must read the PHP manual pages for mysqli_* functions Commented Sep 10, 2012 at 21:51
  • @Palladium: Nearly all questions on SO could be solved by reading docs and searching the net, but that would defeat the purpose of Stack Overflow. The OP has obviously tried something and had a problem with it. That's why we're here. Commented Sep 10, 2012 at 21:56

2 Answers 2

2

Note that $result is not a string, but a MySQLi result.

Use something like

while ($row = $result->fetch_assoc()) {
    /* Process $row here ... */
    var_dump($row);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the solution. Much easier than tossing it into an array and then traversing it.
2

This should work

$tables = array();
$result = mysqli_query($link, "SHOW TABLES");
while (($row = mysqli_fetch_assoc($result)) !== null)
{
        $tables[] = $row[key($row)];
}

1 Comment

exactly what I was describing, but rodion's solution is much simpler. thanks for the input though!

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.