0
我試圖從ajax調用得到與mysql_query中mysql_fetch_array的常規方式常規表值php得到MySQL錶行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
}
}
在php錯誤日誌:
[28 - 8 - 2017 11時25分32秒UTC]計數:1 [28 - 8 - 2017 11時25分32秒UTC] 1
雖然我知道它是2行!
和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();
}
}
});
的問題是:1。 我不知道如何從同時將數據推到數組? 2.我不知道如何循環到這個陣列中的阿賈克斯在html DIV查看呢?
首先停止使用已棄用的版本的mysql_ *'。轉到'mysqli_ *'或者'PDO'(連同'prepared statement'來防止你的SQL注入查詢)。對於當前的代碼做僅此: - 如果'($ comtype == 3){ \t $ getsteps =請求mysql_query( 「SELECT ID FROM步驟WHERE ID = $ ID」); \t $行= mysql_fetch_array($ getsteps); \t $ countsteps = count($ row); \t回波json_encode(陣列( 「comtype」=> $ comtype, 「countsteps」=> $ countsteps, 「步驟」=> $行)); }' –