0

I'm trying to put the tables from MySQL database in a HTML page using PHP.
I'm beginner in PHP and I face a problem to the mysqli_query function.

This is my PHP code:

// connect to the db
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$connection = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to db");

// show the tables
$result = mysqli_query($connection, 'SHOW TABLES') or die ('cannot show    tables');
while($tableName = mysqli_fetch_row($result)) {
  $table = $tableName[0];
  echo '<h3>', $table, '</h3>';
  $result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
  if(mysqli_num_rows($result2)) {
    echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
    echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
    while($row2 = mysqli_fetch_row($result2)) {
      echo '<tr>';
      foreach ($row2 as $key=>$value) {
        echo '<td>',$value, '</td>';
      }
      echo '</tr>';
    }
    echo '</table><br />';
  }
}

Unfortunately I get this error:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test\showtables.php on line 26, cannot show columns.

I've also tried with the mysql_query function, but I faced another error, I then I changed it back to the mysqli_query function.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Caroso
  • 111
  • 3
  • 12
  • 3
    Please, read `mysqli` manual. Especially part for arguments of functions. – u_mulder Jun 13 '16 at 16:36
  • 2
    Possible duplicate of [Mysqli\_Query warning: mysqli\_query() expects parameter 1 to be mysqli](http://stackoverflow.com/questions/10160664/mysqli-query-warning-mysqli-query-expects-parameter-1-to-be-mysqli) – u_mulder Jun 13 '16 at 16:38
  • Follow your first usage, `mysqli_query($connection, `.. Here's the manual: http://php.net/manual/en/mysqli.query.php. Specifically: `mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )`. – chris85 Jun 13 '16 at 16:41
  • Code working fine for me when i test. Is your connection proper? Check db name and other things.. – ankit Jun 13 '16 at 16:46

2 Answers2

2

In the second call to the mysqli_query function, you're passing in the table name instead of the connection. Try something like this:

$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");

http://php.net/manual/en/mysqli.query.php

  • I tried, but now it is diplayed the "die message": cannot show columns..something it's still wrong – Caroso Jun 13 '16 at 17:46
  • Make sure that you're either using double quotes `($connection, "SHOW COLUMNS FROM $table")` or concatenating `($connection, 'SHOW COLUMNS FROM ' . $table)`. See [Difference between single and double quotes in PHP](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – user6459501 Jun 13 '16 at 17:58
2

This code show you all of the tables and tables rows. I try it works

<?PHP
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'testdb';

    $mysqli = new mysqli($host, $user, $pass, $db);

    //show tables
    $result = $mysqli->query("SHOW TABLES from testdb");
    //print_r($result);
    while($tableName = mysqli_fetch_row($result))
    {
        $table = $tableName[0];
        echo '<h3>' ,$table, '</h3>';
        $result2 = $mysqli->query("SHOW COLUMNS from ".$table.""); //$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
        if(mysqli_num_rows($result2))
        {
            echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
            echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
            while($row2 = mysqli_fetch_row($result2))
            {
                echo '<tr>';
                foreach ($row2 as $key=>$value)
                {
                    echo '<td>',$value, '</td>';
                }
                echo '</tr>';
            }
            echo '</table><br />';
        }
    }
?>

Sample Output :

enter image description here

Ivan Barayev
  • 2,035
  • 5
  • 24
  • 30