如何在zend框架中打印准确的sql查询?

时间:2022-12-07 22:48:13

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

我从模型中得到了下面的代码,

    ...
                  $select = $this->_db->select()
                    ->from($this->_name)
                    ->where('shipping=?',$type)
                    ->where('customer_id=?',$userid);
                 echo  $select; exit; // which gives exact mysql query.
            .....

When i use update query in zend like ,

当我在zend中使用update查询时,

$up_value = array('billing'=> '0');
$this->update($up_value,'customer_id ='.$userid.' and address_id <> '.$data['address_Id']);      

Here i want to know the exact mysql query. Is there any possible way to print the mysql query in zend ? kindly advice

这里我想知道确切的mysql查询。有没有可能用zend打印mysql查询?亲切的建议

12 个解决方案

#1


111  

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

Select对象在Zend框架中有一个__toString()方法。

From the Zend Framework manual:

Zend框架手册:

$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.

另一种解决方案是使用Zend_Db_Profiler。即。

$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

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

#2


20  

from >= 2.1.4

从> = 2.1.4

echo $select->getSqlString()

#3


13  

I have traversed hundred of pages, googled a lot but i have not found any exact solution. Finally this worked for me. Irrespective where you are in either controller or model. This code worked for me every where. Just use this

我搜索了几百页,用谷歌搜索了很多,但是我没有找到任何确切的解决方案。最后,这对我起了作用。不管你在控制器或模型中的位置。这个代码适用于我的任何地方。就用这个

//Before executing your query
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->getProfiler()->setEnabled(true);
$profiler = $db->getProfiler();

// Execute your any of database query here like select, update, insert
//The code below must be after query execution
$query  = $profiler->getLastQueryProfile();
$params = $query->getQueryParams();
$querystr  = $query->getQuery();

foreach ($params as $par) {
    $querystr = preg_replace('/\\?/', "'" . $par . "'", $querystr, 1);
}
echo $querystr;

Finally this thing worked for me.

最后这东西对我起作用了。

#4


6  

You can use Zend_Debug::Dump($select->assemble()); to get the SQL query.

您可以使用Zend_Debug::转储($选择- >组装());获取SQL查询。

Or you can enable Zend DB FirePHP profiler which will get you all queries in a neat format in Firebug (even UPDATE statements).

或者,您可以启用Zend DB FirePHP分析器,它将使您在Firebug(甚至更新语句)中获得所有查询的整洁格式。

EDIT: Profiling with FirePHP also works also in FF6.0+ (not only in FF3.0 as suggested in link)

编辑:使用FirePHP进行分析也可以在FF6.0+中使用(不只是在链接中建议的FF3.0中使用)

#5


3  

Now on Zend2:

现在Zend2:

$select->getSqlString();

Displaying the generated SQL from ZendDbSql object

显示从ZendDbSql对象生成的SQL

#6


2  

you can print..

你可以打印. .

print_r($select->assemble());

#7


0  

Use this:-

用这个:

echo $select->query();

or

Zend_Debug::dump($select->query();

#8


0  

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.

查看Zend_Db_Profiler。这允许您在准备和执行任何SQL语句时进行日志记录。它适用于更新语句和选择查询。

#9


0  

$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;

#10


0  

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();

#11


0  

even shorter:

更短:

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

and more shorter:

和更短:

echo  $select .""; die;

#12


-1  

$db->getProfiler()->setEnabled(true);

// your code    
$this->update('table', $data, $where);    
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());    
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());    
$db->getProfiler()->setEnabled(false);

#1


111  

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

Select对象在Zend框架中有一个__toString()方法。

From the Zend Framework manual:

Zend框架手册:

$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.

另一种解决方案是使用Zend_Db_Profiler。即。

$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

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

#2


20  

from >= 2.1.4

从> = 2.1.4

echo $select->getSqlString()

#3


13  

I have traversed hundred of pages, googled a lot but i have not found any exact solution. Finally this worked for me. Irrespective where you are in either controller or model. This code worked for me every where. Just use this

我搜索了几百页,用谷歌搜索了很多,但是我没有找到任何确切的解决方案。最后,这对我起了作用。不管你在控制器或模型中的位置。这个代码适用于我的任何地方。就用这个

//Before executing your query
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->getProfiler()->setEnabled(true);
$profiler = $db->getProfiler();

// Execute your any of database query here like select, update, insert
//The code below must be after query execution
$query  = $profiler->getLastQueryProfile();
$params = $query->getQueryParams();
$querystr  = $query->getQuery();

foreach ($params as $par) {
    $querystr = preg_replace('/\\?/', "'" . $par . "'", $querystr, 1);
}
echo $querystr;

Finally this thing worked for me.

最后这东西对我起作用了。

#4


6  

You can use Zend_Debug::Dump($select->assemble()); to get the SQL query.

您可以使用Zend_Debug::转储($选择- >组装());获取SQL查询。

Or you can enable Zend DB FirePHP profiler which will get you all queries in a neat format in Firebug (even UPDATE statements).

或者,您可以启用Zend DB FirePHP分析器,它将使您在Firebug(甚至更新语句)中获得所有查询的整洁格式。

EDIT: Profiling with FirePHP also works also in FF6.0+ (not only in FF3.0 as suggested in link)

编辑:使用FirePHP进行分析也可以在FF6.0+中使用(不只是在链接中建议的FF3.0中使用)

#5


3  

Now on Zend2:

现在Zend2:

$select->getSqlString();

Displaying the generated SQL from ZendDbSql object

显示从ZendDbSql对象生成的SQL

#6


2  

you can print..

你可以打印. .

print_r($select->assemble());

#7


0  

Use this:-

用这个:

echo $select->query();

or

Zend_Debug::dump($select->query();

#8


0  

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.

查看Zend_Db_Profiler。这允许您在准备和执行任何SQL语句时进行日志记录。它适用于更新语句和选择查询。

#9


0  

$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;

#10


0  

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();

#11


0  

even shorter:

更短:

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

and more shorter:

和更短:

echo  $select .""; die;

#12


-1  

$db->getProfiler()->setEnabled(true);

// your code    
$this->update('table', $data, $where);    
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQuery());    
Zend_Debug::dump($db->getProfiler()->getLastQueryProfile()->getQueryParams());    
$db->getProfiler()->setEnabled(false);