call_user_func_array + bind_result = pass as reference mess
Posted: Tue Nov 13, 2012 10:20 am
I have three types of merchandise on my site, two different kinds of records and merchandise. If a customer is shopping through merchandise and puts an item in his/her cart, the script will realize it is a t-shirt from the merch table and gather variables from the DB including size, color, price, etc. Alternatively, when a customer is looking at records, the needed variables will be title, artist, release date, etc.
Given this need, I set my script up so that it determines what type of merch a user is shopping, and then sets up variable arrays. Then this array is passed to bind_result using call_user_func_array. Since call_user_func_array requires that your args are passed as reference, I am getting stuck with an array full of references that all point to the last set of query results.
Here is the relevant code:
I have been working on this for days. Is there anyone out there who is willing to talk me through this, as opposed to just writing code?
Given this need, I set my script up so that it determines what type of merch a user is shopping, and then sets up variable arrays. Then this array is passed to bind_result using call_user_func_array. Since call_user_func_array requires that your args are passed as reference, I am getting stuck with an array full of references that all point to the last set of query results.
Here is the relevant code:
Code: Select all
function prodArr() {
if (!empty($_POST['cart'])) {
global $dbType;
$prodDetail = array();
$columns = array();
$data = array();
$img = '';
$artist = '';
$title = '';
$label = '';
$year = null;
$color = '';
$size = '';
$sex = '';
$qty = 0;
$price = 0.00;
//check for database type and set up $results (array) and $tbl (string) accordingly
if ($dbType === 'distro') {
$columns = array('img' => $img, 'artist' => $artist, 'title' => $title, 'label' => $label, 'year' => $year, 'price' => $price, 'qty' => $qty);
$tbl = 'products';
}
elseif ($dbType === 'releases') {
$columns = array('img' => $img, 'artist' => $artist, 'title' => $title, 'year' => $year, 'price' => $price, 'qty' => $qty);
$tbl = 'products';
}
elseif ($dbType === 'merch') {
$columns = array('img' => $img, 'title' => $title,'size' => $size, 'color' => $color, 'sex' => $sex, 'price' => $price, 'qty' => $qty);
$tbl = 'merch';
}
//connect to database
$mysqli = Database::getInstance();
//Set up query
$query = 'SELECT ';
$query .= '`'.implode('`, `', array_keys($columns)).'`';
$query .= ' FROM ' . $tbl . ' WHERE (`id` = ?) AND (`qty` > 0) AND (`agedOff` <> 1);';
//query for each item in post array
$i = 0;
foreach ($_POST['cart'] as $key => $reqQty) {
//$stmt = $mysqli->stmt_init();
if (!$stmt = $mysqli->prepare($query)) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param('s', $key)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$results = array();
foreach ($columns as $col) {
$results[$col] = &$data[$i][$col]; //This foreach loop was recommended by a member on another forum. His idea was to create a new reference every time, but I still end up with duplicated references, and my array keys don't persist.
}
if (!call_user_func_array(array($stmt, 'bind_result'), $results)) {
echo "Binding results failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->fetch()) {
echo "Fetching results failed: (" . $stmt->errno . ") " . $stmt->error;
}
$prodDetail[$key] = $results;