0

I am trying to return a multidimensional associative array from my MySql Database with the Format

$array[0]['user']
$array[0]['dateCompleted']
$array[0]['etc']
$array[1]['user']
$array[1]['dateCompleted']
$array[1]['etc']

...

Here is my code:

$mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE);
//Clean input
$idUser = trim($_SESSION['tmp01']);
/* create a prepared statement */
$stmt = $mysqli->prepare("SELECT user, dateCompleted, workType, workPrice FROM workDone WHERE user=? ORDER BY dateCompleted DESC");

/* bind parameters for markers */
$stmt->bind_param("i", $idUser);

$stmt->execute();

$data = $stmt->result_metadata();
$fields = array();
$row = array();
$rows = array();

$fields[0] = &$stmt;
$count = 1;

// this dynamically creates an array where each key is the name of the field. 
while ($field = mysqli_fetch_field($data)) {
    $fields[$count] = &$row[$field->name];
    $count++;
}

// this calls bind_result() to each member of this $row array  
call_user_func_array(array($stmt, 'bind_result'), $row);       //<--problem                      
while ($stmt->fetch())
    array_push($rows, $row);

$results = (count($rows) == 0) ? false : $rows;
//print_r($results);
return $results;

$stmt->close();

It works when I use the SQL statement "SELECT * FROM users ..."), but I overrun my servers memory by doing that. Instead I use the above code, but what it does it return a the same instance for each row I have.

In other words if I have four distinct rows in my MySQL database, it will return the first row four times.

P.S.

My server does not have mysqlnd enabled.

0

1 Answer 1

2

Your approach looks to be correct, but you seem to be passing incorrect arguments to call_user_func_array. Try this:

$data = $stmt->result_metadata();
$fields = array();
$currentrow = array();
$results = array();

// Store references to keys in $currentrow
while ($field = mysqli_fetch_field($data)) {
  $fields[] = &$currentrow[$field->name];
}

// Bind statement to $currentrow using the array of references
call_user_func_array(array($stmt,'bind_result'), $fields);

// Iteratively refresh $currentrow array using "fetch", store values from each row in $results array
$i = 0;
while ($stmt->fetch()) {
  $results[$i] = array(); //this is supposed to be outside the foreach
  foreach($currentrow as $key => $val) {
    $results[$i][$key] = $val;
  }
  $i++;
}

return $results;
8
  • When I use the above code, it only returns an array with a nested array containing only the workPrice key and none of the other fields. Array ( [0] => Array ( [workPrice] => 25.00 ) [1] => Array ( [workPrice] => 25.00 ) [2] => Array ( [workPrice] => 100.00 ) )
    – AlexHeuman
    Commented Dec 26, 2012 at 23:02
  • @AlexHeuman I actually just spotted a syntax error in my code (extra parenthesis in call_user_func_array). That shouldn't have run at all. Commented Dec 26, 2012 at 23:07
  • Right, I spotted the same syntax and removed it before I implemented it, my only problem is the not being able to return all of the fields into an array, but only the last one. Looking at the code, I'm unable to understand what to change.
    – AlexHeuman
    Commented Dec 26, 2012 at 23:07
  • @AlexHeuman Are the workPrice values correct according to the data present in the table? I'm trying to figure it out, but as a wild guess, try using call_user_func_array(array($stmt,'bind_result'), $currentrow); Commented Dec 26, 2012 at 23:09
  • 1
    Alright, working like it was never broken. Thanks for all of your help and debugging. It's always the smallest things that cause the biggest problems.
    – AlexHeuman
    Commented Dec 26, 2012 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.