0

即使数据库中没有任何内容,$query_check_homework 也会一直失败。我确信这可能是一个非常简单的问题,比如语法错误,但我已经为此工作了一个多小时并且无法使其正常工作。您能否看一下代码,让我知道为什么即使数据库中没有与查询匹配的内容,它仍然会返回一行。

这是代码。

/*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."')");
4

1 回答 1

0

根据您使用的数据库连接类,$query_check_homework 可能只包含一个指向结果集的指针,并且总是“==1”或“true”,仅仅是因为不为 false 或 0 .

相反,考虑使用一个函数来计算从查询返回的结果数:

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

于 2013-05-28T02:43:07.730 回答