繁体   English   中英

如何在javascript函数中使用php的mysql数据库中的值

[英]how to use the values from mysql database from php in javascript function

在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)。 这个怎么做?

尝试这样的事情:

<?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>

祝好运!!

在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> */
  }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM