2013-10-05 83 views
0

我建立簡單的WebService我的web應用程序只包含兩個PHP文件connecttodatabase.php包含MySQL連接的代碼和的index.php包含我的web服務的代碼,但我得到了SQL錯誤:MySQL服務器版本問題

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 'desc, cat, price FROM itmes' at line 3

connecttodatabase.php

<?php 
$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
$pos = strpos($requesturi, "localhost:99/self/index.php"); 

$hostname = "localhost"; 
$database = "self"; 
$username = "root"; 
$password = ""; 

$self = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 

的index.php

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 

     <?php 
//get data 
require_once('connecttodatabase.php'); 
mysql_select_db($database, $self); 

//build query 
$query = 
    "SELECT 
    name, 
    desc, 
    cat, 
    price 
    FROM itmes"; 

$rsPackages = mysql_query($query, $self) or 
    die(mysql_error()); 

$arRows = array(); 
while ($row_rsPackages = mysql_fetch_assoc($rsPackages)) { 
    array_push($arRows, $row_rsPackages); 
} 

header('Content-type: application/json'); 
echo json_encode($arRows); 


?> 

    </body> 
</html> 

回答

0

這是因爲您在選擇時使用了命令DESC,該命令引發異常。使用特殊符號,以防止這些問題:

`something` 

所以,你的代碼都會有這樣的形式:

$query = 
    "SELECT 
    `name`, 
    `desc`, 
    `cat`, 
    `price` 
    FROM `itmes`"; 

,它應該是工作。