How to use prepared statements in the Zend Framework

Mysql supports prepared statements as follows:

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

Is there support in the Zend Framework (I could not find it) and how to use it. If not, how would you implement the prepared instructions as a complement to the Zend Framework.

+3
source share
4 answers

After some research, I did not find ZF support for prepared statements in the same way. The only thing you can do is imitate him, as @Nikita Gopkalo posted.

+1
source
$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());
+3
source

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

zend.

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

so here is a sample code.

<?php

use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;

$statement = $driver->createStatement('SELECT * FROM users');
$statement->prepare();
$result = $statement->execute($parameters);

if ($result instanceof ResultInterface && $result->isQueryResult()) {
  $resultSet = new ResultSet;
  $resultSet->initialize($result);

  foreach ($resultSet as $row) {
    echo $row->my_column . PHP_EOL;
  }
}

?>
0
source

All Articles