如何从php中的mysql发送数据到javascript数组

时间:2022-12-21 21:33:46

php:

$servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "mydb";

                $conn = new mysqli($servername, $username, $password, $dbname);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                        } 
$sql = "SELECT * from inventory_list";
$result=$conn->query($sql);
$row=array();
if ($result->num_rows > 0) {
    // output data of each row
while($row=$result->fetch_assoc())

    {    printf("NAME: %s ID: %s",$row['name'],$row['serialno']);



    }


} else {
    echo "0 results";
}
$conn->close();
}

That was the php part of my code. so i need to save all the rows retrieved from mysql to a javascript global array? How can i do that ?

这是我的代码的PHP部分。所以我需要将从mysql检索到的所有行保存到javascript全局数组中?我怎样才能做到这一点 ?

2 个解决方案

#1


0  

As others have suggested, you can use JSON. If you're looking for a place to start, you can take a look at php's json_encode() http://php.net/manual/en/function.json-encode.php

正如其他人所建议的那样,您可以使用JSON。如果你正在寻找一个可以开始的地方,你可以看一下php的json_encode()http://php.net/manual/en/function.json-encode.php

You may need to set the correct headers too: header('Content-Type: application/json');

您可能还需要设置正确的标题:header('Content-Type:application / json');

#2


0  

try this, you need a JQuery libraries.

试试这个,你需要一个JQuery库。

JQuery:

$.ajax({
   url: 'your_php_filename',
   type: 'POST',
   data: {},
   success:function(result) {
      console.log(result)//to check the value of result in array form
   }
})

PHP:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "mydb";

  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  } 
  $sql = "SELECT * from inventory_list";
  $result = $conn->query($sql);
  $row = [];
  if ($result->num_rows > 0) {
    // output data of each row
    while($row=$result->fetch_assoc()) {    
       $row = "NAME: ".$row['name']."ID: ".$row['serialno'];
    }
  } else {
     echo "0 results";
  }
  $conn->close();
}
?>

#1


0  

As others have suggested, you can use JSON. If you're looking for a place to start, you can take a look at php's json_encode() http://php.net/manual/en/function.json-encode.php

正如其他人所建议的那样,您可以使用JSON。如果你正在寻找一个可以开始的地方,你可以看一下php的json_encode()http://php.net/manual/en/function.json-encode.php

You may need to set the correct headers too: header('Content-Type: application/json');

您可能还需要设置正确的标题:header('Content-Type:application / json');

#2


0  

try this, you need a JQuery libraries.

试试这个,你需要一个JQuery库。

JQuery:

$.ajax({
   url: 'your_php_filename',
   type: 'POST',
   data: {},
   success:function(result) {
      console.log(result)//to check the value of result in array form
   }
})

PHP:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "mydb";

  $conn = new mysqli($servername, $username, $password, $dbname);
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  } 
  $sql = "SELECT * from inventory_list";
  $result = $conn->query($sql);
  $row = [];
  if ($result->num_rows > 0) {
    // output data of each row
    while($row=$result->fetch_assoc()) {    
       $row = "NAME: ".$row['name']."ID: ".$row['serialno'];
    }
  } else {
     echo "0 results";
  }
  $conn->close();
}
?>