Is there a way to execute a SQL String as a query in Zend Framework?
有办法在Zend框架中执行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_Select
object from it "by hand". Or if thats possible create a Zend_Db_Table_Select
object 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 doc中没有找到解决方法。
4 个解决方案
#1
22
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 : http://framework.zend.com/manual/en/zend.db.statement.html
如果在开始时创建Zend_DB对象,可以使用它创建查询。看看手册中的这一条目:http://framework.zend.com/manual/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);
#2
2
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();
#3
2
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对等物(如truncatetable / INSERT SELECT语句)的查询是有用的。
#4
2
Here is an example for ZF1:
以下是ZF1的一个例子:
$db =Zend_Db_Table_Abstract::getDefaultAdapter();
$sql = "select * from user"
$stmt = $db->query($sql);
$users = $stmt->fetchAll();
#1
22
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 : http://framework.zend.com/manual/en/zend.db.statement.html
如果在开始时创建Zend_DB对象,可以使用它创建查询。看看手册中的这一条目:http://framework.zend.com/manual/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);
#2
2
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();
#3
2
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对等物(如truncatetable / INSERT SELECT语句)的查询是有用的。
#4
2
Here is an example for ZF1:
以下是ZF1的一个例子:
$db =Zend_Db_Table_Abstract::getDefaultAdapter();
$sql = "select * from user"
$stmt = $db->query($sql);
$users = $stmt->fetchAll();