簡體   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