开发者

Why doesn't this query produce a mysql_error() result?

I was running some code & I wasn't getting any errors, but the row wasn't being deleted either.... so I was a bit confused. So I checked out the code & found out I had a problem with my query, but at the same time it wasn't producing a true result to my test with mysql_error().

I am using the be开发者_JS百科low code..

        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());                    

        }

Here is where the problem is & it doesn't enter the mysql_error() section..

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);
    }

}

Here is the code from those functions I used..

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);
        }

    }

}


If you delete 0 rows, it will likely still be considered a success. Use mysql_affected_rows to make sure you actually did anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜