获取SQL数组时出错

时间:2015-04-21 11:10:15

标签: php mysql sql

我对PHP非常陌生,我对这个简单任务的错误感到沮丧。我想从SQL数据库导入一个表并将其显示在HTML表中。但是在尝试获取表列名时我一直遇到错误。

虽然我测试了与数据库的连接。

错误是:

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.

我在w3schools上找到了这个示例,我在php.net

上通过其他示例进行了编辑

如果有人可以帮助我,我会很感激。

<?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>";
?>

1 个答案:

答案 0 :(得分:1)

Table是一个保留关键字,您无法像这样使用它。如果你想从名为users的表中获取一些数据或者像这样的表,那么查询应该是 -

select * from users