澄清我的数据库称为数据库,我的表称为表(这是为了测试)。
class Database
{
public $server = "localhost";
public $database = "database";
public $user = "root";
public $password = "";
public $row;
public $result;
//call connection method upon constructing
public function __construct(){
$this->createConnection();
}
//connection to the database
public function createConnection()
{
$this->dbLocalhost = mysql_connect($this->server, $this->user, $this->password)
or die("could not connect:".mysql_error());
mysql_select_db($this->database)
or die("Could not find the database: ".mysql_error());
}
//execute query string
public function query($queryString)
{
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
$this->result = mysql_query($queryString)
or die($queryString."<br/><br/>".mysql_error());
while($this->row = mysql_fetch_array($this->result)) {
echo "<tr>";
echo '<td>' . $this->row['id'] . '</td>';
echo '<td>' . $this->row['firstname'] . '</td>';
echo '<td>' . $this->row['lastname'] . '</td>';
echo '<td><a href="edit.php?id=' . $this->row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $this->row['id'] . '">Delete</a></td>';
echo "</tr>";
}
echo "</table>";
}
然后我有调用函数的 php 文件。
<?php
include('database.php');
$db = new Database();
$sql = "SELECT *
FROM table";
$db->query($sql);
?>
这是收到的错误:
从表中选择 *
您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 2 行的“表”附近使用的正确语法
谢谢。
最佳答案
table
是一个 reserved word在 MySQL 中,所以你必须转义它。您可以通过在其周围放置 `
来做到这一点。
例如:
SELECT * FROM `table`
正如您在我的链接中看到的,有几个棘手的保留字可能会引起很多麻烦。有人说在编写表名和字段名时应始终使用 `
字符。
关于php - 试图从数据库中查看表,但显然我的语法不正确,尽管它与 PHPmyAdmin 上的查询匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13336719/