¿Cómo formar una matriz en do-while?
Frecuentes
Visto 82 veces
-1
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":""}]"
1 Respuestas
0
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 php arrays json or haz tu propia pregunta.
1) Use a straight
while
en lugar dedo..while
, the latter just makes things more verbose here. 2) Perhaps you have the wrong fetch mode? Trymysql_fetch_assoc
en lugar defetch_array
. 3) Strongly consider migrating away from themysql
extension -- it is old and has been deprecated. - Jon1. 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 tomysql_fetch_array()
. - Oswaldtry to get formed json :) - Darien Fawkes
You get formed json :) Again, where is the problem? - Oswald
@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 :) - IMSoP