开发者

What is the best way to use PDO prepared statements?

When using prepared statements in PDO should I "build" 开发者_如何学Goa prepared statement for every db call or can I use one statement for all calls? example:

 class DB{
    ...
    function query($sqlStatementString,$data){
        $this->sth->prepare($sqlStatementString);
        $this->sth->execute($data);
    }
    ...
}

OR

class User{
    ...
    function doSomething(){
        $sthForDoSomething->prepare(...);
        $sthForDoSomething->execute(...);
    }
    ...
    function jump(){
        $sthForJump->prepare(...);
        $sthForJump->execute(...);
    }
}

Are there memory/speed implications of using one method over the other? thanks!


If you are ever going to issue the same query more than once, with only different parameters bound to the placeholders, you should try to structure your code in such a way that you can reuse the statement. Calling prepare causes the database to create and cache an execution plan for the query which you will reuse in future calls to execute, making those queries faster.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜