问题描述
在php中,我使用了sql查询来获取数据:-
$sql="Select * from xyz";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
}
现在,我想使用在javascipt函数中使用$ row [“ id”]获得的所有值,并将其用于变量中(可以说var j)。 这个怎么做?
1楼
angelcool.net
0
2015-08-07 05:39:51
尝试这样的事情:
<?php
$sql="Select * from xyz";
$errorsData = array();
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
$errorData[]=$row["id"]
}
$errorJson = json_encode($errorsData);
?>
<script>
var errorJson = <?= $errorJson ?> ;
console.log(errorJson);
<script>
祝好运!!
2楼
sanjeev shetty
0
2015-08-07 05:45:45
在jQuery中,我会这样做,
PHP
$sql="Select * from xyz";
$arr_json = array();
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
error_log( "id" . $row["id"]);
$arr_json[] = $row['id'];
}
回声json_encode($ arr_json);
jQuery的
$.get(
<THAT CALLED URL>,
function(ret_data){
console.log(ret_data);
/* you can use the ret_data as ret_data.<INDEX> */
}
);