如何在Zend Framework中使用预准备语句

时间:2012-04-11 10:23:34

标签: php mysql zend-framework prepared-statement

Mysql以这种方式支持预处理语句:

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

在Zend Framework中是否有对它的支持(我找不到它),以及如何使用它。如果没有,您将如何实现准备好的语句作为Zend Framework插件。

4 个答案:

答案 0 :(得分:3)

$sql = "SELECT * FROM table_name WHERE id = :id ";

$stmt  = Zend_Registry::get("db")->prepare($sql);

$data=array(array('id'=> $id);

$stmt->execute($data);

print_r($stmt->fetchAll());

答案 1 :(得分:1)

您可以尝试这样

$sql = "SELECT * FROM table_name WHERE id = :id'";
$stmt = new Zend_Db_Statement_Pdo($this->_db, $sql); 
$stmt->execute(array(':id' => $id));

答案 2 :(得分:1)

经过一番研究,我没有找到ZF对预备语句的支持。你唯一能做的就是仿效@Nikita Gopkalo。

答案 3 :(得分:0)

您应该查看zend文档。

https://github.com/zendframework/zend-db/blob/master/doc/book/result-set.md

所以这是一个示例代码。

List<Integer> doubles = MyStream.of(Stream.of(1.0, 3.0, 7.0, 2.0, 9.0)) // create instance
                .biggerThanFour() //call MyStream methods
                .stream(doubleStream -> doubleStream.map(aDouble -> aDouble * 2)) //Do youre base stream intermediate methods and return again MyStream so you can call more specific custom methods
                .biggerThanFourteen()
                .stream() // call the base stream more or less your delegate for last intermediate methods and terminal method
                .mapToInt(Double::intValue)
                .boxed() //Ah if you have IntStreams and similar you can call the boxed() method to get an equivalent stream method.
                .collect(Collectors.toList()); // terminal method call