【问题标题】:Why doesn't this query produce a mysql_error() result?为什么这个查询不产生 mysql_error() 结果?
【发布时间】:2011-06-29 23:48:06
【问题描述】:

我正在运行一些代码并且我没有收到任何错误,但该行也没有被删除......所以我有点困惑。所以我检查了代码并发现我的查询有问题,但同时它并没有为我的 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);
        }

    }

}

【问题讨论】:

  • 有什么问题?你能提供实际的错误信息吗?当您直接在 mysql 控制台中尝试查询时会发生什么?
  • 你能试试mysql_error($dblink)吗?
  • 别告诉我问题出在DELET ...
  • @Pekka mysql_error() 不带参数假定最近执行的 mysql 函数,因此它应该在没有它的情况下工作。
  • @ring0:他特别指出'所以我检查了代码并发现我的查询有问题'。他知道查询是错误的。他想知道为什么 if 语句中的代码没有运行。

标签: php mysql oop try-catch


【解决方案1】:

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

【讨论】:

  • 我认为这在使用事务时效果不佳。至少这是我的经验。虽然这可能是因为我是在整个事务之后才这样做的,这是有道理的,哈哈——尽管由于查询格式不正确,这不应该仍然引发错误吗?
  • 如果你出错,它会给你一个 -1,如果没有行改变,它会给你一个 0,或者行数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多