[英]Return multiple rows of an array from a mysqli query
I have this in my functions.php file 我在我的functions.php文件中有这个
function getUserOrders($userId){
global $conn;
$query = "SELECT * ";
$query .= "FROM orders ";
$query .= "WHERE userid=" . $userId . " ";
$odrset = mysqli_query($conn, $query);
while ($odr = mysqli_fetch_assoc($odrset)){
return $odr;
}
}
What I neeed to do in my orders.php file is display specific fields and their values from the returned $odr array as this snippet suggests 我在orders.php文件中需要做的是显示特定字段及其返回的$ odr数组中的值,因为此片段建议
$userId = sql_prep($_SESSION['userid']) ;
getUserOrders($userId);
echo $odr['title'].$odr['orderid'].'<br>'
I am only able to do it in the functions.php file... 我只能在functions.php文件中执行此操作...
function getUserOrders($userId){
global $conn;
$query = "SELECT * ";
$query .= "FROM orders ";
$query .= "WHERE userid=" . $userId . " ";
$odrset = mysqli_query($conn, $query);
confirm_query($odrset);
while ($odr = mysqli_fetch_assoc($odrset)){
echo $odr['title'].$odr['orderid'].'<br>';
}
}
..and calling it in my orders.php file like so: ..并在我的orders.php文件中调用它,如下所示:
$userId = sql_prep($_SESSION['userid']) ;
getUserOrders();
which is not good since i need to recycle the function somewhere else and display different fields and their values. 这是不好的,因为我需要在其他地方回收功能并显示不同的字段及其值。 So I need to have $odr returned as an array in my order.php 所以我需要在我的order.php中将$ odr作为数组返回
Store it as an array and then return the array. 将其存储为数组,然后返回该数组。
function getUserOrders($userId){
global $conn;
$query =
"SELECT *
FROM orders
WHERE userid= ?";
$odrset = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($odrset, 'i', $userId);
mysqli_stmt_execute($odrset);
while ($odr = mysqli_fetch_assoc($odrset)){
$return[] = $odr;
}
return $return;
}
I've updated your mysqli
connection to use a parameterized query with prepared statement. 我已经更新了你的mysqli
连接,以使用带有mysqli
准备语句的参数化查询。 You can read more about these here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php . 您可以在这里阅读更多相关信息, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php 。 This is the preferred approach than escaping. 这是逃避的首选方法。
Later usage... 以后使用......
$orders = getUserOrders($_SESSION['userid']);
foreach($orders as $order) {
echo $order['title'] . $order['orderid'];
}
You may not need the sql_prep
function with this approach, I'm not sure what that did. 你可能不需要使用这种方法的sql_prep
函数,我不知道它做了什么。 Your questions code didn't pass the userid
to the function so I don't think that was your exact usage. 您的问题代码未将userid
传递给函数,因此我认为这不是您的确切用法。
mysqli_fetch_assoc only returns one record at a time so you need to store the results inside an array and return the array from the function: mysqli_fetch_assoc一次只返回一条记录,因此您需要将结果存储在数组中并从函数中返回数组:
// functions.php
function getUserOrders($userId){
global $conn;
$query = "SELECT * ";
$query .= "FROM orders ";
$query .= "WHERE userid=" . $userId . " ";
$odrset = mysqli_query($conn, $query);
$results = array();
while ($odr = mysqli_fetch_assoc($odrset)){
$results[] = $odr;
}
return $results;
}
// in your orders file
$userid = sql_prep($_SESSION['userid']);
$orders = getUserOrders($userid);
foreach ($order as $orders) {
echo $order['title']. $order['orderid'] . '<br>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.