Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am starting with Zend Framework 2 , I want to make a routing choice with the role of My user and I must write getRoleByID($id) ,

then How can'I write

" Select 'role' from user where ('id' = $id) " with Zend\Db\Sql

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Example Using Select:

$select = new \Zend\Db\Sql\Select('user');
$select->columns(array('role'));
$where = new Where();
$where->equalTo('id', $id);
$select->where($where);
/**
 * Simple example of executing a query...
 */
$stmt = $this->getSql()->prepareStatementForSqlObject($select);
$results = $stmt->execute();
/* @var $results \Zend\Db\Adapter\Driver\Pdo\Result */

if( ! $results->count()) {
     // do something, none found...
}

$row = $results->current(); 
return $row['role'];

// if you had multiple results to iterate over:
//$resultSet = new \Zend\Db\ResultSet\ResultSet();
//$resultSet->initialize($results);    
//$array = $resultSet->toArray();
//foreach($resultSet as $row) { /* ... */ }
share|improve this answer
    
thank you for your reponce , it's very helpful . Just one question , i must execute the query with // $stmt = $this->getSql()->prepareStatementForSqlObject($select); $results = $stmt->execute(); // to can compare with the result ?? –  Loouu Jun 28 '13 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.