gpt4 book ai didi

php - 从 mysql 表获取行到 php 数组并将其传递给 ajax

转载 作者:行者123 更新时间:2023-11-29 10:38:49 24 4
gpt4 key购买 nike

我正在尝试使用 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

虽然我知道它是 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();
}
}
});

问题是: 1.我不知道如何将while中的数据插入数组? 2.我不知道如何在ajax中循环进入这个数组以在中查看它 分区?

最佳答案

问题是您在 while 循环之前调用 mysql_fetch_array()。这将获取第一行,循环将获取剩余的行。

如果要测试循环之前是否返回任何行,请调用mysql_num_rows()。您还可以使用它来代替 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
}
}

关于php - 从 mysql 表获取行到 php 数组并将其传递给 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45918146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com