I have the following code:
$sql = "CALL bd.spPrueba()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row[] = $result->fetch_assoc()) {
$json = json_encode($row);
}
} else {
echo "0 results";
}
echo $json;
$conn->close();
And it gives me the following result:
[{"Clave":"79","Tiempo":"02:23:43","Estatus":"Si","FechaInicio":"2017-03-06 13:02:41"}]
But if I want to get each result to store it in a variable, that is to say this:
$sql = "CALL bd.spPrueba()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row[] = $result->fetch_assoc()) {
$Clave = $row["Clave"];
$Tiempo = $row["Tiempo"];
echo "<br>";
echo "Clave = ".$Clave;
echo "<br>";
echo "Tiempo = ".$Tiempo;
$json = json_encode($row);
}
} else {
echo "0 results";
}
echo $json;
$conn->close();
It just doesn't work and it doesn't save me anything, it just shows me the following:
Clave =
Tiempo =
[{"Clave":"79","Tiempo":"02:23:43","Estatus":"Si","FechaInicio":"2017-03-06 13:02:41"}]
Unless you remove the square brackets from the variable $row
in the while
ie like this:
$sql = "CALL bd.spPrueba()";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$Clave = $row["Clave"];
$Tiempo = $row["Tiempo"];
echo "<br>";
echo "Clave = ".$Clave;
echo "<br>";
echo "Tiempo = ".$Tiempo;
$json = json_encode($row);
}
} else {
echo "0 results";
}
echo $json;
$conn->close();
And it would give me the following result:
Clave = 79
Tiempo = 02:23:43
{"Clave":"79","Tiempo":"02:23:43","Estatus":"Si","FechaInicio":"2017-03-06 13:02:41"}
If I make avar_dump();
In $row[]
I can see the following:
array(1) { [0]=> array(8) { ["Clave"]=> string(2) "79" ["Tiempo"]=> string(8) "02:50:30" ["Estatus"]=> string(5) "Si" ["FechaInicio"]=> string(19) "2017-03-06 13:29:27" } }
In row
I can see the following:
array(8) { ["Clave"]=> string(2) "79" ["Tiempo"]=> string(8) "02:51:04" ["Estatus"]=> string(5) "Si" ["FechaInicio"]=> string(19) "2017-03-06 13:30:01" }
I would like to leave the brackets but to save the fields in variables php
because if I remove the brackets, the arrayJson
one I have in Android crashes.
I think what is happening is that you are saving an object
row
in each position of thearray
row[]
, so you should be able to enter with the index and the name of the field, when you print try this.If you print something with it then take an iterator so you can save the values.
Try this
It shouldn't give you a problem afterwards.