How to print exact sql query in zend framework ?

前端 未结 14 1964
栀梦
栀梦 2020-12-07 09:33

I have the following piece of code which i taken from model,

    ...
                  $select = $this->_db->select()
                    ->from($th         


        
相关标签:
14条回答
  • 2020-12-07 10:15

    Select objects have a __toString() method in Zend Framework.

    From the Zend Framework manual:

    $select = $db->select()
                 ->from('products');
    
    $sql = $select->__toString();
    echo "$sql\n";
    
    // The output is the string:
    //   SELECT * FROM "products"
    

    An alternative solution would be to use the Zend_Db_Profiler. i.e.

    $db->getProfiler()->setEnabled(true);
    
    // your code
    $this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']); 
    
    Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());
    Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());
    $db->getProfiler()->setEnabled(false);
    

    http://framework.zend.com/manual/en/zend.db.select.html

    0 讨论(0)
  • 2020-12-07 10:17

    Now on Zend2:

    $select->getSqlString();
    

    Displaying the generated SQL from ZendDbSql object

    0 讨论(0)
  • 2020-12-07 10:19
    $statement = $this->sql->getSqlStringForSqlObject( HERE GOES Zend\Db\Sql\SelectSQL object );
    
    echo "SQL statement: $statement";
    

    Example:

    $select = $this->sql->select();
    ...
    $select->from(array( 'u' => 'users' ));
    $select->join(...
    $select->group('u.id');
    ...
    $statement = $this->sql->getSqlStringForSqlObject($select);
    echo $statement;
    
    0 讨论(0)
  • 2020-12-07 10:19

    Check out the Zend_Db_Profiler. This allows you to log any SQL statement as it is prepared and executed. It works for UPDATE statements as well as SELECT queries.

    0 讨论(0)
  • 2020-12-07 10:21

    even shorter:

    echo $select->__toString()."\n";
    

    and more shorter:

    echo  $select .""; die;
    
    0 讨论(0)
  • 2020-12-07 10:24

    I have done this by this way

    $sql = new Sql($this->adapter);
            $select = $sql->select();
            $select->from('mock_paper');
            $select->columns(array(
                'is_section'
            ));
            $select->where(array('exam_id = ?' => $exam_id,'level_id = ?' => $level_id))->limit(1);
    
    
    
            $sqlstring = $sql->buildSqlString($select);
            echo $sqlstring;
            die();
    
    0 讨论(0)
提交回复
热议问题