所以这是我当前的代码:更好的方法来做到这一点?
function addPage($uniquename, $ordernum, $title, $author, $content, $privilege, $description=NULL, $keywords=NULL){
if (!$description) $description = NULL;
if (!$keywords) $keywords = NULL;
//UPDATE `table` SET `ordernum` = `ordernum` + 1 WHERE `ordernum` >= 2
$query = "UPDATE ".$this->prefix."page SET ordernum = ordernum+1 WHERE ordernum >= ?";
if ($stmt = $this->db->prepare($query)){
$stmt->bind_param("i", $ordernum);
$stmt->execute();
if (!arCheck($stmt)) return false;
} else {
$this->stmtError("addPage", $stmt->error);
}
$query = "INSERT INTO ".$this->prefix."page VALUES (LCASE(?), ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = $this->db->prepare($query)){
$stmt->bind_param("sisisssi", $uniquename, $ordernum, $title, $author, $content, $description, $keywords, $privilege);
$stmt->execute();
return arCheck($stmt);
} else {
$this->stmtError("addPage", $stmt->error);
}
}
它是假设一个新的页面添加到数据表。 MySQL是由Phil Hunt提供的Store the order of something in MySQL
我知道你可以使用多重查询来完成同样的事情,但是我被告知准备好的语句在性能和安全性方面更好。有没有另一种方法来做到这一点?像准备好的多重查询?
另外,如何做交易?我不完全确定这是什么,我假设它是,如果我们说,INSERT语句失败,它也会撤消UPDATE语句?
注意:arCheck函数将关闭语句。
当你的功能需要比3至5个参数更多的时候,它就是一种代码味道。您可能想要为所有不同的内容项目使用数组。另一种代码异味是大括号的不一致使用。我建议始终使用它们。 – markus 2010-11-01 05:51:57