2013-05-28 62 views
0

$ query_check_homework保持失敗。我相信這可能是一個非常簡單的東西,就像語法錯誤,但我一直在這工作了一個多小時,並且無法工作。你可以看看代碼,讓我知道爲什麼它會一直返回一行,即使數據庫中沒有任何內容與查詢匹配。儘管數據庫中沒有任何內容,MySQL仍保留返回行,即使數據庫中沒有任何內容與查詢匹配

這是代碼。

/*Check if homework name already exists*/ 
$query_check_homework = $this->db_connection->query(" 
SELECT 
    homework.homework_name 
FROM classes 
    INNER JOIN homework On classes.class_id = homework.class_id 
WHERE 
    homework.class_id = '".$this->class_id."' And 
homework.homework_name = '".$this->homework_name."' And 
classes.user_id = '".$this->user_id."'");  

if ($query_check_homework == 1) { 
    $this->errors[] = "Sorry, You already have homework in this class with that name. Please choose a different name."; 
} 
else { 
    /*write new homework into the datebase*/ 
    $query_new_homework = $this->db_connection->query(" 
    INSERT INTO homework (class_id, homework_name, avaliable_points, earned_points,  date) VALUES ('".$this->class_id."', '".$this->homework_name."', '".$this- >avaliable_points."', '".$this->earned_points."', '".$this->homework_date."')"); 

回答

0

很有可能,這取決於數據庫的連接您正在使用類,即$ query_check_homework只包含一個指向一個結果集,始終將「== 1」或「真」,僅僅憑藉的不是假的,或0

相反,可以考慮用它計算從查詢返回的結果數的函數:

http://php.net/manual/en/mysqli-result.num-rows.php

+0

感謝。我忘了在最後添加num_rows函數。它應該看起來像'$ query_check_user_name-> num_rows == 1' – user2363217

相關問題