显示 PHP MySQL 中函数返回的数据
Display data returned by function in PHP MySQL
这是我第一次尝试的事情。我在 PHP 和 MySQL 中创建了几个网站作为数据库,但从未使用函数从数据库中获取数据。
但这是我现在正在尝试的所有新事物,因为现在我想为一个沼泽项目实现 reusability
。我想通过 PHP Function
从数据库(即 MySQL
)获取订单详细信息。我对如何打印 PHP 函数返回的值感到困惑。
我写的函数如下:
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
$result = DB::instance()->prepare($sql)->execute()->fetchAll();
return $result;
}
请告诉我如何打印这些值,上面函数返回的值是一个数组。
我尝试通过 print_r(_orderdetails());
直接调用函数
请告诉我:
- 迭代此函数以打印值的有效方法。
- 有没有其他方法可以更好地工作?
在将调用此函数的 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 />';
}
这样您将从您正在使用的数据库 table 回显 1 列。
您也可以使用->fetch();
而不是->fetchAll();
还有一些其他函数也可以作为 worker 遍历 table 中的下一行。
您可以在以下位置查看 PDO 以获得更多功能:
您不能像这样将 fetchAll() 链接到 execute()。
此外,对于不带参数的查询,使用 execute() 是没有意义的。所以像这样改变你的功能
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
return DB::instance()->query($sql)->fetchAll();
}
因此您将能够以最自然的方式迭代结果
foreach (_orderdetails() as $order) {
echo $order['id'] . '<br />';
}
这是我第一次尝试的事情。我在 PHP 和 MySQL 中创建了几个网站作为数据库,但从未使用函数从数据库中获取数据。
但这是我现在正在尝试的所有新事物,因为现在我想为一个沼泽项目实现 reusability
。我想通过 PHP Function
从数据库(即 MySQL
)获取订单详细信息。我对如何打印 PHP 函数返回的值感到困惑。
我写的函数如下:
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
$result = DB::instance()->prepare($sql)->execute()->fetchAll();
return $result;
}
请告诉我如何打印这些值,上面函数返回的值是一个数组。
我尝试通过 print_r(_orderdetails());
直接调用函数请告诉我:
- 迭代此函数以打印值的有效方法。
- 有没有其他方法可以更好地工作?
在将调用此函数的 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 />';
}
这样您将从您正在使用的数据库 table 回显 1 列。
您也可以使用->fetch();
而不是->fetchAll();
还有一些其他函数也可以作为 worker 遍历 table 中的下一行。
您可以在以下位置查看 PDO 以获得更多功能:
您不能像这样将 fetchAll() 链接到 execute()。
此外,对于不带参数的查询,使用 execute() 是没有意义的。所以像这样改变你的功能
function _orderdetails(){
$sql = "Select * from orders WHERE 1";
return DB::instance()->query($sql)->fetchAll();
}
因此您将能够以最自然的方式迭代结果
foreach (_orderdetails() as $order) {
echo $order['id'] . '<br />';
}