2014-02-22 91 views
0
function get_subject_by_id($subject_id){ 
    global $connection; 
    $query = "SELECT * "; 
    $query .= "FROM subjects "; 
    $query .= "WHERE 'id' ={$subject_id} "; 
    $query .= "LIMIT 1 "; 
    $result_set = mysql_query($query, $connection); 
    confirm_query($result_set); 
    // if no rows are returned fetch_array will return false 
    if(!is_null($result_set)){ 
    echo "got here"; 
    echo $result_set; 
    } 
    if($subject=mysql_fetch_array($result_set)){ // not working properly 
     echo "testing fetch array"; 
     return $subject; 
    }else { 
     return NULL; 
    } 

雖然我的表中有3個項目,但提取查詢仍然返回false。一些代碼顯然正在測試,看看我是否得到了結果,並且這些工作都正常,但是fetch數組並沒有執行if語句,因爲它認爲它是錯誤的。請幫助無奈的幾個小時屬於這個問題mysql提取數組保持返回false

回答

3

您在查詢中使用了錯誤的引號類型,它們應該是ba ckticks:

$query .= "WHERE `id` ={$subject_id} "; 
       ^^ 

行情使id一個字符串,而不是列名。由於字符串id永遠不會等於$subject_id,查詢不會返回任何內容。

+0

感謝您分享,它更有意義,爲什麼它不會工作,這解決了我在客戶端的問題。謝謝。 – chacliff

1

我想這是因爲標識列單引號括起來,而不是反引號

$query .= "WHERE 'id' ={$subject_id} "; 

應該

$query .= "WHERE id ={$subject_id} "; 

或者

$query .= "WHERE `id` ={$subject_id} ";