为什么这个查询不会产生mysql_error()结果?

时间:2011-02-18 17:26:15

标签: php mysql oop try-catch

我正在运行一些代码&我没有收到任何错误,但行也没有被删除....所以我有点困惑。所以我检查了代码&发现我的查询有问题,但同时它没有使用mysql_error()为我的测试产生真正的结果。

我正在使用以下代码..

        try {
            // Start transaction
            beginTransaction($this->db_connection);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }

        try {

            // Delete main entry
            $this->removeMainEntry($lid);

            // Delete list columns
            $this->removeListColumns($lid);

            // Commit changes to database
            commitChanges($this->db_connection);

        } catch (Exception $e) {

            try {
                // Rollback changes
                rollback($this->db_connection, $e->getMessage());
            } catch (Exception $re) {
                throw new Exception($re->getMessage());
            }

            throw new Exception($e->getMessage());                    

        }

以下是问题所在&它不会进入mysql_error()部分..

protected function removeMainEntry($lid) {

    $lid = (int) $lid;

    // Remove from database
    $query = "DELET FROM lists WHERE id=" . $lid . " LIMIT 1";
    $sql = mysql_query($query, $this->db_connection);

    if (mysql_error()) {
        $etext = 'Problem removing list main entry from database.';
        $log_error = $etext . ' MySQL Error: ' . mysql_error() . '. Query: ' . $query;
        log_site_error($log_error);
        throw new Exception($etext);
    }

}

以下是我使用的那些函数的代码..

if (!function_exists('beginTransaction')) {

    function beginTransaction($dblink) {

        $query = "BEGIN";
        mysql_query($query, $dblink);

        if (mysql_error()) {
            $etext = 'Problem adding new list data to database.';
            $log_error = $etext . ' MySQL Error: ' . mysql_error() . '. Query: ' . $query;
            log_site_error($log_error);
            throw new Exception($etext);            
        }

    }

}

if (!function_exists('commitChanges')) {

    function commitChanges($dblink) {

        $query = "COMMIT";
        mysql_query($query, $dblink);

        if (mysql_error()) {
            $etext = 'Problem adding new list data to database.';
            $log_error = $etext . ' MySQL Error: ' . mysql_error() . '. Query: ' . $query;
            log_site_error($log_error);
            throw new Exception($etext);            
        }

    }    

}

if (!function_exists('rollback')) {

    function rollback($dblink, $error = '') {

        $query = "ROLLBACK";
        mysql_query($query, $dblink);

        if (mysql_error()) {
            $etext = $error . ' Additionally there was a problem rolling back the changes in the database. Please check the logs.';
            $log_error = $etext . ' MySQL Error: ' . mysql_error() . '. Query: ' . $query;
            log_site_error($log_error);
            throw new Exception($etext);
        }

    }

}

1 个答案:

答案 0 :(得分:3)

如果删除0行,可能仍会被视为成功。使用mysql_affected_rows确保您确实做了任何事情。