php - Return multiple rows of an array from a mysqli query -
i have in functions.php file
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 neeed in orders.php file display specific fields , values returned $odr array snippet suggests
$userid = sql_prep($_session['userid']) ; getuserorders($userid); echo $odr['title'].$odr['orderid'].'<br>'
i able in functions.php file...
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 in orders.php file so:
$userid = sql_prep($_session['userid']) ; getuserorders();
which not since need recycle function somewhere else , display different fields , values. need have $odr returned array in order.php
store array , return array.
function getuserorders($userid){ global $conn; $query = "select * orders 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 mysqli
connection use parameterized query prepared statement. can read more these here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. preferred approach escaping.
later usage...
$orders = getuserorders($_session['userid']); foreach($orders $order) { echo $order['title'] . $order['orderid']; }
you may not need sql_prep
function approach, i'm not sure did. questions code didn't pass userid
function don't think exact usage.
Comments
Post a Comment