[英]get rows from mysql table to php array and pass it to ajax
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 我正在尝试通过常规方式从php中的 Ajax调用mysql_query和mysql_fetch_array从常规表中获取值
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
}
}
[28-Aug-2017 11:25:32 UTC] count: 1 [28-Aug-2017 11:25:32 UTC] 1 [2017年8月28日11:25:32 UTC]计数:1 [2017年8月28日11:25:32 UTC] 1
while the count I know it is 2 rows!!! 虽然我知道这是2行!
$.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? 问题是:1.我不知道如何将数据从数组推入数组? 2. I don't know how to loop into this array in ajax to view it in html div? 2.我不知道如何在ajax中循环进入此数组以在html div中查看它?
The problem is that you're calling mysql_fetch_array()
before the while
loop. 问题是您在while
循环之前调用了mysql_fetch_array()
。 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()
. 如果要在循环之前测试是否返回了任何行,请调用mysql_num_rows()
。 You can also use this instead of count($steps_array)
. 您也可以使用它代替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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.