I'm trying to get values from a regular table with regular way of mysql_query and mysql_fetch_array from ajax call in php
if ($comtype==3){
$getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");
$row = mysql_fetch_array($getsteps);
if($row != false){
$steps_array = array();
while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC))
{
array_push($steps_array,$row);
}
$countsteps = count($steps_array);
error_log("count: ".$countsteps);
error_log(print_r($steps_array));
echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array));
} else {
//error
}
}
in php error log :
[28-Aug-2017 11:25:32 UTC] count: 1 [28-Aug-2017 11:25:32 UTC] 1
while the count I know it is 2 rows!!!
and in ajax
$.ajax({
type:'POST',
url:'app/check.php',
data: {'ID':ID},
dataType: 'json',
success: function(value){
if (value.comtype== "0") {
$("#newC").show();
$("#hadC").hide();
} else {
var method;
var com;
com = "<div class='clearfix'></div><br><h3>old.</h3>";
com += "By Stpes: <br>";
for (i = 0; i < value.countsteps; i++) {
com += "Step " + Number(i)+1 + ": Greater Than " + value.steps_array['calfrom'] + " AND Less Than or Equal " + value.steps_array['calto'] + " Apply " + value.steps_array['calapl'] + " % <br>";
}
}
$("#hadC").html(com);
$("#hadC").show();
$("#newC").hide();
}
}
});
question is: 1. I don't know how to push data from while into array? 2. I don't know how to loop into this array in ajax to view it in html div?
The problem is that you're calling mysql_fetch_array()
before the while
loop. This fetches the first row, and the loop fetches the remaining rows.
If you want to test whether any rows were returned before looping, call mysql_num_rows()
. You can also use this instead of count($steps_array)
.
if ($comtype==3){
$getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");
$countsteps = mysql_num_rows($getsteps);
if($countsteps > 0) {
$steps_array = array();
while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC))
{
array_push($steps_array,$row);
}
error_log("count: ".$countsteps);
error_log(print_r($steps_array));
echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array));
} else {
//error
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.