mardi 4 avril 2017

How to create an array during stmt->fetch() on a prepared statement?

I'm working with PHP on a new project, I'm pretty new on PHP and coming from Qt, mysqli seems really complicated, I have a prepared query which returns correctly some arrays during stmt->fetch() but I'm not able to retrieve those arrays so I can use them as variables in my code.

Here is the piece of code:

$stmt = $mysqli->prepare("SELECT nombre FROM platos where menuid=? OR menuid=? OR menuid=? OR menuid=?");
$stmt->bind_param('iiii', intval($opcion1id), intval($opcion2id), intval($opcion3id), intval($opcion4id));
$stmt->execute();

function stmt_bind_assoc (&$stmt, &$out) {
    $data = mysqli_stmt_result_metadata($stmt);
    $fields = array();
    $out = array();
    $fields[0] = $stmt;
    $count = 1;

    while($field = mysqli_fetch_field($data)) {
        $fields[$count] = &$out[$field->name];
        $count++;
    }                   
    call_user_func_array(mysqli_stmt_bind_result, $fields);
}

$stmt->store_result();
$resultrow = array();
stmt_bind_assoc($stmt, $resultrow);
$stmt->store_result();

while($stmt->fetch()){
   var_dump($resultrow);
}

$stmt->close();

The var_dump() of the array $resultrow is this:

array(1) {
  ["nombre"]=>
  &string(14) "Carne Estofada"
}
array(1) {
  ["nombre"]=>
  &string(15) "Merluza Plancha"
}
array(1) {
  ["nombre"]=>
  &string(12) "Pasta Tomate"
}
array(1) {
  ["nombre"]=>
  &string(15) "Verdura Hervida"
}

I can't manage to access each of these strings out of the while, I've been looking for documentation for days but I can't seem to manage to solve it.




Aucun commentaire:

Enregistrer un commentaire