問題描述
有沒有辦法在 Zend Framework 中將 SQL 字符串作為查詢執(zhí)行?
Is there a way to execute a SQL String as a query in Zend Framework?
我有一個這樣的字符串:
I have a string like that:
$sql = "SELECT * FROM testTable WHERE myColumn = 5"
現(xiàn)在我想直接執(zhí)行這個字符串,而不是解析它并從它手動"創(chuàng)建一個 Zend_Db_Table_Select
對象.或者,如果可能的話,從該字符串創(chuàng)建一個 Zend_Db_Table_Select
對象,以執(zhí)行該對象.
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 文檔中沒有找到解決方案.
How can I do that? I didn't find a solution for this in the Zend doc.
推薦答案
如果您在開始時創(chuàng)建 Zend_DB 對象,您可以使用它創(chuàng)建查詢.看看手冊中的這個條目:https://framework.zend.com/manual/1.12/en/zend.db.statement.html
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
$stmt = $db->query(
'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
array('goofy', 'FIXED')
);
或
$sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
這篇關(guān)于使用 Zend 框架的原始 SQL 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!