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?
mysql_*
. turn tomysqli_*
ORPDO
(along withprepared statement
to prevent your query from SQL Injection). for current code do only this:-if ($comtype==3){ $getsteps = mysql_query("SELECT id FROM steps WHERE id = $id"); $row = mysql_fetch_array($getsteps); $countsteps = count($row); echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $row)); }