为什么这个查询不产生 mysql_error() 结果? [英] Why doesn't this query produce a mysql_error() result?
问题描述
我正在运行一些代码 &我没有收到任何错误,但该行也没有被删除......所以我有点困惑.所以我检查了代码 &发现我的查询有问题,但同时它并没有为我的 mysql_error() 测试产生真正的结果.
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 below 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());
}
这就是问题所在&它没有进入 mysql_error() 部分..
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);
}
}
}
推荐答案
如果你删除了 0 行,它仍然可能被认为是成功的.使用 mysql_affected_rows 确保您确实做了任何事情.
If you delete 0 rows, it will likely still be considered a success. Use mysql_affected_rows to make sure you actually did anything.
这篇关于为什么这个查询不产生 mysql_error() 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!