如何在Zend 1中创建查询并从查询中获取结果?

时间:2014-04-15 07:11:21

标签: sql zend-framework

我试图查看查询,执行它并获得结果。 这是我的尝试:

$last_id = $_COOKIE['last_id'];
//if (isset($last_id)){
$db = new Zend_Db_Table('order_products_names'); //the table name
$query = "select * from order_products_names where order_id = '$last_id'";
$query = $db->query($query);
while ($row = $query->fetch()) {
    echo $row['names'];
}
显然它不起作用。有人可以帮我弄这个吗 ? 我也试过这个版本:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
                'host'     => 'localhost',
                'username' => 'lunchbox_live',
                'password' => 'xxxxxxxxxxxx',
                'dbname'   => 'order_products_names',
                'adapterNamespace' => 'MyProject_Db_Adapter'
));
 $last_id = $_COOKIE['last_id'];
 $dbAdapter = Zend_Db_Table::getDefaultAdapter();
 $query = $dbAdapter->query("select * from order_products_names where order_id = '$last_id'";

当此行($query = $dbAdapter->query("select * from order_products_names where order_id = '$last_id'";)代码被注释时会发生这种情况: enter image description here 当没有评论时会发生这种情况: enter image description here

2 个答案:

答案 0 :(得分:0)

这不是一个好办法,但只是回答你的问题,尝试这样的事情:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
                'host'     => 'localhost',
                'username' => 'lunchbox_live',
                'password' => 'xxxxxxxxxxxx',
                'dbname'   => 'order_products_names',
                'adapterNamespace' => 'MyProject_Db_Adapter'
));

$last_id = $_COOKIE['last_id'];
$stmt = $db->query("select * from order_products_names where order_id = ?", array($last_id)); 
$products = $stmt->fetchAll();

foreach($products as $product)
{
//....
}

答案 1 :(得分:0)

尝试:

$db = Zend_Db_Table::getDefaultAdapter();
$row = $db->fetchRow("SELECT * FROM order_products_names where order_id = '". $last_id . "'");
echo $row['names'];