简体   繁体   English

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

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

In php, i have used an sql query to fetch the data like :- 在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"]);

    }

Now i want to use all the values attained using $row["id"] in javascipt function and use it in a variable (var j , lets say). 现在,我想使用在javascipt函数中使用$ row [“ id”]获得的所有值,并将其用于变量中(可以说var j)。 How to do this? 这个怎么做?

Try something like this: 尝试这样的事情:

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

Good luck!! 祝好运!!

in jquery I would have done like this, 在jQuery中,我会这样做,

php 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'];
    }

echo json_encode($arr_json); 回声json_encode($ arr_json);

jquery 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.

相关问题 使用PHP从数据库中提取值并将结果作为变量传递给JavaScript - Use PHP to extract values from database and pass the results as a variable to JavaScript 如何使用 JAVASCRIPT 从 Input 标签中获取值,输入标签的值是使用 php 从 mysql 数据库中获取的 - How to get values from Input tag using JAVASCRIPT ,the value of the input tags are fetched from mysql database using php 如何使用PHP从Javascript到MySQL数据库获取信息? - How to get information from Javascript to MySQL database using PHP? 如何从 MySQL 数据库获取 php 日期到 javascript? - How to get php date from MySQL database to javascript? 将JavaScript中的值直接插入MySQL数据库 - Inserting values from JavaScript directly into MySQL database 将值从mysql数据库获取到wordpress中的javascript中 - getting values from mysql database into javascript in wordpress javascript-来自mysql数据库的变量值 - javascript - variable values from mysql database 如何在JavaScript中将JSON数组值转换为整数(来自php / mysql) - How to cast JSON array values as integers in javascript (from php / mysql) 如何创建一个将从MySQL数据库中删除表行的JavaScript函数 - How to make a JavaScript function that will delete table rows from a MySQL database PHP中来自MySQL数据库的Javascript数组 - Javascript array from mysql database in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM