这是我第一次绝对尝试的一件事。我做了很少的网站在PHP和MySQL作为DB,但从来没有使用函数从数据库中获取数据。
但这是我正在尝试所有新的东西,因为现在我想要实现一个沼泽项目的reusability。我想通过PHP Function从DB (即PHP Function)获得订单细节。对于如何打印PHP函数返回的值,我感到有点困惑。
我写的函数如下:
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
$result = DB::instance()->prepare($sql)->execute()->fetchAll();
return $result;
}请告诉我如何打印这些值,上面的函数返回的值是一个数组。
我尝试通过print_r(_orderdetails())直接调用函数;
请让我知道:
发布于 2017-01-21 16:51:22
不能像这样链接fetchAll()来执行()。
此外,对于不接受参数的查询,使用execute()没有意义。所以像这样改变你的功能
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
return DB::instance()->query($sql)->fetchAll();
}因此,您将能够以最自然的方式迭代结果
foreach (_orderdetails() as $order) {
echo $order['id'] . '<br />';
}发布于 2015-05-16 20:15:41
在调用此函数的PHP代码中,请使用以下命令
print_r(_orderdetails());
//to get all the result set... I mean all the values coming from this function.您还可以在函数调用之后使用该函数并使用->,然后将变量名如下所示:
To iterate through the values of this function:
$value_arr = _orderdetails();
foreach ($valur_arr as $value) {
echo $value . '<br />';
}这样,您将从正在使用的数据库表中回显1列。
发布于 2016-01-25 09:01:39
您也可以使用->fetch();
而不是->fetchAll();
还有一些其他函数也作为工作人员来迭代表中的下一行。
您可以在以下站点查看PDO以获得更多功能:
http://php.net/manual/en/book.pdo.php
https://stackoverflow.com/questions/30280160
复制相似问题