zend 2中的mysql函数

时间:2015-05-26 06:30:55

标签: mysql zend-framework zend-framework2

如何在zend 2中查询以下

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

感谢任何帮助。 感谢

3 个答案:

答案 0 :(得分:0)

使用以下内容:

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

有关详细信息,请参阅 HereHere

答案 1 :(得分:0)

/* 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);

答案 2 :(得分:0)

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

资料/型号/的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
        }

}       

资料/控制器/索引控制器

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


    }


}

祝你好运