2012-11-11 2 views
3

데이터베이스를 데이터베이스라고하고 테이블을 테이블이라고합니다 (테스트 용).데이터베이스에서 테이블을 보려고했지만 PHPmyAdmin의 쿼리와 일치하는데도 불구하고 잘못된 구문이 있습니다.

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); 

?> 

이것은 수신 오류입니다 :

SELECT *을 테이블에서

당신은 당신의 SQL 구문에 오류가 있습니다; 귀하의 MySQL 서버 버전에 해당하는 설명서를 확인하여 2 행에서 '테이블'근처에서 올바른 구문을 사용하십시오.

감사합니다.

+0

은 테이블이라고 불리는 테이블입니까?!?! – Andbdrew

+0

그래, 데이터베이스가 데이터베이스 – normower

답변

2

table은 MySQL에서 reserved word이므로, 반드시 이스케이프해야합니다. 그 주위에 `을 넣음으로써 그것을 할 수 있습니다. 예를 들어

: 내 링크를 볼 수있는

SELECT * FROM `table` 

, 두통을 많이 일으킬 수있는 몇 가지 까다로운 예약어가 있습니다. 테이블과 필드 이름을 쓸 때 항상 `자를 사용해야한다고 말하는 사람들도 있습니다.

+0

라고하면 나는 그냥받습니다 : SELECT * FROM 'table' SQL 구문에 오류가 있습니다. MySQL 서버 버전에 해당하는 설명서를 확인하여 올바른 구문이 2 행에서 '테이블'근처에서 사용되도록하십시오. – normower

+1

@ user1816869 아니요, 다른 문자입니다. 내 말은 '~이 아니라'. 그것은 백틱이라고합니다. en-us 키보드의 왼쪽 위 모서리. – kapa

+0

와우, 바보 같은 느낌, 나는 새 이름으로 새 테이블을 만들었 어. 도움을 주셔서 감사합니다 – normower

관련 문제