¿Cómo formar una matriz en do-while?

I try to form array in the do-while:

$result = mysql_query("SELECT * FROM data WHERE company='$companyID'", $db);
        if (mysql_num_rows($result) > 0) 
        {       
            $resultData = mysql_fetch_array($result);
            do
            {           
                $json[] = array('product' => $resultData['product'], 'title' => $resultData['title']); 
            }       
            while($resultData = mysql_fetch_array($result));
            echo json_encode($json);
        }

In exit all datais empty(error):

"[{"product":"","title":""},{"product":"","title":""},{"product":"","title":""},{"product":"","title":""}]"

preguntado el 09 de septiembre de 13 a las 23:09

1) Use a straight while en lugar de do..while, the latter just makes things more verbose here. 2) Perhaps you have the wrong fetch mode? Try mysql_fetch_assoc en lugar de fetch_array. 3) Strongly consider migrating away from the mysql extension -- it is old and has been deprecated. -

1. What do you expect? and why? From what I see, there are perfectly valid reasons for you getting the output that you get. 2. Put var_export($resultData) into the loop to see how the array is structured, that you get from the call to mysql_fetch_array(). -

try to get formed json :) -

You get formed json :) Again, where is the problem? -

@Oswald The problem is that all the values in the JSON are empty, probably due to Jon's point 2, which really should be an answer not a comment :) -

1 Respuestas

does it actually give you an error?

try printing out your output to see if you actually fetch anything from mysql

$result = mysql_query("SELECT * FROM data WHERE company='".$companyID."'", $db);
        if (mysql_num_rows($result) > 0) 
        {       

            while($resultData = mysql_fetch_array($result))
            {
              echo '<pre>';
              print_r($resultData);
              echo '</pre>';
            }
        }

if you do get output that way just replace the content between {} of while with echo json_encode($json);

espero que esto ayude.

Respondido 17 Oct 13, 00:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.