简体   繁体   English

尝试从数据库中查看表,但显然我的语法不正确,尽管它与PHPmyAdmin上的查询匹配

[英]trying to view a table from a database, but apparently i have the incorrect syntax, despite it matching the query on PHPmyAdmin

to clarify my database is called database and my table is called table (this was for testing). 澄清我的数据库被称为数据库,我的表被称为表(这是用于测试)。

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

and then i have php file which calls the functions. 然后我有调用函数的php文件。

<?php

include('database.php');
$db = new Database();
$sql = "SELECT * 
    FROM table"; 
$db->query($sql);

?>

This is the error a receive: 这是收到的错误:

SELECT * FROM table SELECT * FROM表

You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table' at line 2 检查与MySQL服务器版本对应的手册,以便在第2行的“table”附近使用正确的语法

Thanks. 谢谢。

table is a reserved word in MySQL, so you must escape it. table是MySQL中的保留字 ,所以你必须将其转义。 You can do that by putting ` s around it. 您可以通过将做到这一点`周围秒。

For example: 例如:

SELECT * FROM `table`

As you can see on my link, there are several tricky reserved words that can cause a lot of headache. 正如你在我的链接上看到的那样,有几个棘手的保留字会引起很多麻烦。 Some say you should always use ` characters when you write table and field names. 有人说你在写表和字段名时应该总是使用`字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从PHP发送时,此查询中显然存在SQL语法错误,但在PHPMyAdmin上运行良好 - Apparently there's an SQL Syntax error in this query when sent from PHP, but it runs fine on PHPMyAdmin 为什么当我尝试了一段时间在 phpMyadmin 数据库的表中创建列时,它不起作用? - why when I have been trying for some time to create columns in a table in a phpMyadmin database, does it not work? 我正在尝试显示来自phpmyadmin数据库的图像和消息 - I'm trying to display an image and a message from phpmyadmin database 我从 phpmyadmin 环境中导出数据库时出错 - i have a error in exporting database from phpmyadmin Environment 我想使用 phpmyadmin 从数据库中导出一个表 - i want to export a single table from the database witout using phpmyadmin 试图从数据库中删除一行(phpmyadmin)onclick - Trying to delete a row from database(phpmyadmin) onclick 我有SQLSRV错误:“,”附近的语法不正确 - I have error SQLSRV : Incorrect syntax near ',' phpmyadmin:MySQL的表行数不正确 - phpmyadmin: Incorrect table rowcount with MySQL 我已经在 Windows 服务器 2019 上安装了 Apache 服务器,并试图限制 phpMyAdmin 从服务器外部访问 - I have installed Apache server on Windows server 2019 and am trying to restrict phpMyAdmin from access outside of server 从phpMyAdmin数据库表获取重复项 - Getting Duplicates from phpMyAdmin Database Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM