0
我是PHP新手,想知道是否有人可以举例说明如何改进此代码?提高sql查询函数性能
public function createContent($title, $content, $category_id){
try {
$user_id = $_SESSION["user_id"];
if(!$user_id){
return(false);
}
$sql = "INSERT INTO content (title, content, user_id, category_id)
VALUES (:title, :content, :user_id, :category_id)";
$query = $this->_db->prepare($sql);
$execute_array = array(
':title' => $title,
':content' => $content,
':user_id' => $user_id,
':category_id' => $category_id
);
$query->execute($execute_array);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
你单挑这种方法因特殊原因?即你是否已经知道这种方法比应该更慢/更耗费资源? – VolkerK 2012-02-13 10:39:43
我看起来很好。如果你做了100个插入,那么将它们“散装”成1个更大的插入可能是一个好主意,但是如果它是临时的,那么你的代码看起来不错。 – FreudianSlip 2012-02-13 10:40:05
如果您多次调用此函数,只需调用'_db-> prepare'一次,因为这可能是您可能考虑的唯一因素。 – Benoit 2012-02-13 10:40:13