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
include('database.php');
$db = new Database();
$sql = "SELECT *
FROM table";
$db->query($sql);
?>
This is the error a receive:
SELECT * FROM table
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 2
Thanks.
tableis a reserved word in MySQL, so you must escape it. You can do that by putting`s around it.For example:
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.