2012-02-11 99 views
-1

我試圖從數據庫提取成員信息,並得到這個錯誤:PHP 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 '' FROM users WHERE user_id = 0' at line 8 That user does not exist.

我不知道要改變什麼,因爲我是新來的PHP。

我的錯誤代碼是:

<?php 

function fetch_users() { 

    $result = mysql_query('SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`'); 

    $users = array(); 

    while (($row = mysql_fetch_assoc($result)) !== false) { 
     $users[] = $row; 
    } 

    return $users; 
} 

function fetch_user_info($uid) { 
    $uid = (int)$uid; 

    $sql = "SELECT 
       `user_username` AS `username`, 
       `user_firstname` AS `firstname`, 
       `user_lastname` AS `lastname`, 
       `user_email` AS `email`, 
       `user_about` AS `about`, 
       `user_location` AS `location`, 
       `user_gender` AS `gender`' 
      FROM `users` 
      WHERE `user_id` = {$uid}"; 

    $result = mysql_query($sql);    

    /*Echo mysql_error();*/ 


    return mysql_fetch_assoc($result);   
} ?> 

回答

3

在第二個查詢,在最後一行結束前FROM子句中,有一個單引號(')是不應該在那裏,嘗試將其取出:

`user_gender` AS `gender`' 
FROM `users` 

而且,這正是MySQL是告訴你在它的錯誤信息: ''FROM users WHERE user_id = 0' at線8

請注意消息中查詢示例開始處的雙重單引號(什麼術語):第一個標記樣本的開始(它總是由兩個單引號括起來),第二個已經是令牌造成麻煩。

0

據我所知,它看起來像是在gender之後的反向符號後立即有一個單引號。除此之外,我認爲這會奏效。

+0

對不起,這裏是我的新錯誤信息:你的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'FROM'用戶附近使用正確的語法WHERE'user_id'='0''在第9行 – user1155141 2012-02-11 21:01:12