php 使用 Zend 框架的原始 SQL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6161370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:31:58  来源:igfitidea点击:

RAW SQL Query with Zend Framework

phpmysqlsqlzend-framework

提问by Matthias B

Is there a way to execute a SQL String as a query in Zend Framework?

有没有办法在 Zend Framework 中将 SQL 字符串作为查询执行?

I have a string like that:

我有一个这样的字符串:

$sql = "SELECT * FROM testTable WHERE myColumn = 5"

now I want to execute this string directly withput parsing it and creating a Zend_Db_Table_Selectobject from it "by hand". Or if thats possible create a Zend_Db_Table_Selectobject from this string, to execute that object.

现在我想直接执行这个字符串,而不是解析它并Zend_Db_Table_Select“手动”从它创建一个对象。或者,如果可能的话,Zend_Db_Table_Select从该字符串创建一个对象,以执行该对象。

How can I do that? I didn't find a solution for this in the Zend doc.

我怎样才能做到这一点?我在 Zend 文档中没有找到解决方案。

回答by JohnP

If you're creating a Zend_DB object at the start you can create a query using that. Have a look at this entry in the manual : https://framework.zend.com/manual/1.12/en/zend.db.statement.html

如果您在开始时创建 Zend_DB 对象,您可以使用它创建查询。看看手册中的这个条目:https: //framework.zend.com/manual/1.12/en/zend.db.statement.html

$stmt = $db->query(
            'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
            array('goofy', 'FIXED')
        );

Or

或者

$sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);

回答by Matthias B

If you are using tableGateway, you can run your raw SQL query using this statement,

如果您使用的是 tableGateway,则可以使用此语句运行原始 SQL 查询,

$this->tableGateway->getAdapter()->driver->getConnection()->execute($sql);

where $sql pertains to your raw query. This can be useful for queries that do not have native ZF2 counterpart like TRUNCATE / INSERT SELECT statements.

其中 $sql 与您的原始查询有关。这对于没有本地 ZF2 对应项(如 TRUNCATE / INSERT SELECT 语句)的查询很有用。

回答by Faiyaz Alam

Here is an example for ZF1:

以下是 ZF1 的示例:

$db =Zend_Db_Table_Abstract::getDefaultAdapter();
$sql =    "select * from user"
$stmt = $db->query($sql);
$users =  $stmt->fetchAll();

回答by besin

You can use the same query in Zend format as

您可以使用 Zend 格式的相同查询作为

$select = db->select()->from(array('t' => 'testTable'))
                     ->$where= $this->getAdapter()->quoteInto('myColumn = ?', $s);
$stmt = $select->query();
$result = $stmt->fetchAll();