-1

I am extremely new to PHP and I'm frustrated by the error of this simple task. I want to import a table from an SQL database and show it in a HTML table. But I keep getting errors when trying to fetch the table column names.

The connection with the database is made though, I tested that.

The error is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table' at line 1.

I found this example on w3schools which I edited by other examples on php.net

If anybody can help me with this, I'd appreciate it.

<?php
    error_reporting(-1);
    $con = mysqli_connect('localhost', 'user', 'pass');
    mysqli_select_db($con, 'database') or die("Could not found" . mysqli_error($con));
    $query = ("select * from Table");
    $result = mysqli_query($con, $query) or die ( mysqli_error ($con) );

    //Print table
    echo "<table>";
        echo "<tr>";
            if($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                //Print headers
                foreach($row as $key => $value ){
                    echo "<td>" . $key . "</td>";
                }
                echo "</tr>";
            }

        $result = mysqli_query($con, $query) or die (mysqli_error());
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            echo "<tr>";
            while( list($key, $value) = each($row)){
                //Print value
                echo "<td>" . $value . "</td>";
            }

            echo "<td>" . $value . "<i class='fa fa-caret-up'></i><i class='fa fa-caret-down'></i></td>";
            echo "</tr>";
        }
    echo "</table>";
?>
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
Vasco
  • 191
  • 1
  • 2
  • 12
  • 5
    Thats a weird table_name , you should change your table name from `Table` to something else, its a reserved word in mysql. – Abhik Chakraborty Apr 21 '15 at 11:11
  • Curious; what's that exact URL to that tutorial you've been following, I'd like to see that. Plus, where "table" was taken from. http://www.w3schools.com/sql/sql_select.asp states `SELECT column_name,column_name FROM table_name;` so it probably didn't come from there. – Funk Forty Niner Apr 21 '15 at 11:30
  • @Fred-ii- It was a combination of multiple. It started with this one: http://www.w3schools.com/php/php_mysql_select.asp. Then I was googling, looking on stackoverflow for methods. And then I created this. – Vasco Apr 21 '15 at 11:35
  • Well, somebody didn't give you the right time of day somewhere. I have to admit that even PHP.net doesn't give concise instructions of things not to do lol – Funk Forty Niner Apr 21 '15 at 11:37

1 Answers1

1

Table is a reserved keyword and you cant use it like this. If you want to fetch some data from a table named users or some like this then the query should be -

select * from users
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • 2
    *"Table is a reserved keyword and you cant use it like this."* - Not entirely true. It just requires special care. http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html *"Reserved words are permitted as identifiers if you quote them as described in Section 9.2, “Schema Object Names”:"* http://dev.mysql.com/doc/refman/5.5/en/identifiers.html - So, quoting the manuals is always good to add in an answer. – Funk Forty Niner Apr 21 '15 at 11:17
  • @Fred-ii- you are right. we can use them but not in the way it is used here. – Sougata Bose Apr 21 '15 at 11:21
  • I knew it was something like that. Thank you. – Vasco Apr 21 '15 at 11:25