Mysql zend 2 中的函数

Mysql functions in zend 2

zend 2下面如何查询

select * from states st where TRIM(LOWER(st.state_name))='noida'

感谢任何帮助。 谢谢

使用以下内容:

$resultStates=$this->states->select()->where('TRIM(LOWER(st.state_name))=?','noida')
    ->query()
    ->fetchAll();

详情参考Here and Here.

/* DB Adapter get and SQL object create */
$adapter = GlobalAdapterFeature::getStaticAdapter();
$sql = new \Zend\Db\Sql\Sql($adapter);

/* Select object create */
$select = new \Zend\Db\Sql\Select();
$select->from('states');
$select->where->addPredicate(
    new \Zend\Db\Sql\Predicate\Expression(
        'TRIM(LOWER(state_name)) = ?',
        'noida'
    )
);

/* Select object convert to string and execute */
$queryString = $sql->getSqlStringForSqlObject($select);
$result = $adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE);

在您的模型文件中,只需使用下面的代码,我使用的是模块配置文件。

Profile/Model/Common.php

namespace Profile\Model;

use Zend\Db\Sql\Sql;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;

class Common 
{
        protected $dbConfig;
        protected $adapter;

        public function __construct($dbConfig)
         {
            $this->adapter = new Adapter($dbConfig);
         }
        public function getStateList()
         {

            $sql  = "select * from states st where TRIM(LOWER(st.state_name))='noida'";
            $statement = $this->adapter->query($sql);

            $results = $statement->execute();

            $resultSet = new ResultSet();
            $resultSet->initialize($results);
            $list = $resultSet->toArray();

            return $list; // This will return a list of array
        }

}       

Profile/Controller/IndexController

namespace Profile\Controller;
use Profile\Model\Common;


class IndexController extends AbstractActionController
{


    protected $dbConfig = array(
                        'driver' => DRIVER,
                        'database' => DB,
                        'username' => DBUSER,
                        'password' => DBPASS
                     );

    public function init(){
    $ssOrder = new Container(__CLASS__);

        //SET OPTIONS 
    }


    public function indexAction()
    {
        $plist = new Common($this->dbConfig);
        $resultList = $plist->getStateList(); // This will give you state list


    }


}

祝你好运