问题
I have the following statements but they return an empty result set:
$sql = 'SELECT * FROM `industry` WHERE `code` LIKE ?';
$statement = $this->getAdapter()->createStatement($sql, array('A_'));
$statement->execute();
What am I doing wrong? I really don't want to use the Zend\Db\Sql\Sql, as it is very verbose.
On a related point, where can I go to find out more about the theory of operation for Zend\Db? It's absolutely maddening. Why does it sometimes return a driver result? Sometimes a ResultSet? How can you view the complete SQL (after quoting, but before execution?) Etc...
回答1:
OK, I had missed the fact that a Result is iterable (as well as a ResultSet). Therefore, assigning the result of $statement->execute() to a variable, then iterating that variable, sorted out the problem.
Futhermore, you can call getResource()
on the result object, and from there access the underlying object (in this case, a PDO Statement). This means you can do things like result->getResource()->fetchAll()
;
回答2:
Try with this query.
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
class tableNameTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
Publice function getIndustry(){
$adapter = $this->tableGateway->getAdapter();
$statement = $adapter->query("Your Query");
$results = $statement->execute();
return $results;
}
}
来源:https://stackoverflow.com/questions/20320790/zf2-how-to-do-this-simple-query-with-zend-db