Pergunta

I have a query that is supposed to get two rows of data from a database, a and b. When I vardump inside of the mysqli_stmt_fetch loop, I get the data of a and b and then inside of the loop, I assign it as a member of an array ($Array[] = $result). However, when I then vardump $Array, the data for b is in both indexes of the array.

Expected result is that the data for both a and b are in the array, but there is just two instances of b's data.

Code

if ($Query = mysqli_prepare($MySQLObj, "SELECT x.c, x.d, x.e, x.f, y.g, y.h FROM x INNER JOIN y ON (y.i = x.j) WHERE x.k = 0 OR x.k = 1 ORDER BY x.l ASC LIMIT 10;")) // Prepare Query
        {

            mysqli_stmt_execute($Query); // Execute query
            mysqli_stmt_bind_result($Query, $Data["m"], $Data["n"], $Data["o"], $Data["p"], $Data["q"], $Data["r"]); // Bind result to array elements

            while (mysqli_stmt_fetch($Query)) // Fetch result
            {

                $DataArray[] = $Data; // This doesn't work
                //var_dump($Data);

            }

            mysqli_stmt_close($Query); // Close query

        }

        var_dump($DataArray);
Foi útil?

Solução

normal you can not _bind_result to an $Data["m"], $Data["n"], $Data["o"], ... array

you need variables e.g. $Data_m , $Data_n ....

but you can set a $variable like that

    $Query->store_result();

    $variables = array();
    $data = array();

      $variables[] = &$Data["m"];
      $variables[] = &$Data["n"];
      $variables[] = &$Data["o"];
        ....

   call_user_func_array(array($Query, 'bind_result'), $variables);

Update

If you don't want to set $variables[] by hand, try

 $meta = $Query->result_metadata();

 while($field = $meta->fetch_field())
        $variables[] = &$Data[$field->name]; 

Outras dicas

Just change $DataArray[] = $Data; into $DataArray[] = &$Data; and thats all.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top