2个查询在jQuery ajax调用?

I am trying to retrieve some data from a Mysql database and put the results in a form.

it works fine when i query the database once and return a result. But when i use the result to run another query and then try to return the result i get a 500 error?

The script and its load-data.php file looks like this :

<script>
$(document).ready(function(){
  
  $("button").click(function(){
    var dataCount = $('#curr_pair').val();
    //console.log(dataCount);
    var var_data = "";
                     $.ajax({
                         url: "http://192.168.2.8/wp-content/plugins/trader/load-data.php",
                         type: 'POST',
                          data: ({  dataNewCount: dataCount }),
                          dataType: 'json',
                          success: function(data) {
                              //console.log(data[1]);
                              //var dataObj = JSON.parse(data);
                              console.log(data);
                              //var test = data;
                              //console.log(test);
                              $("#input").val(data[0]);
                              $("#input1").val(data[1]);
                             //$('#result').html(data)
                          }
                      });
 


$("#comments").load("../wp-content/plugins/trader/load-data.php", {
      dataNewCount: dataCount
    });




  });

});



  </script>
<?php
$server = "localhost";
$username ="XXXXX";
$password = "XXXXX";
$database = "XXXX";

$conn = mysqli_connect($server,$username,$password,$database);
$conn1 = mysqli_connect($server,$username,$password,$database);
$dataNewCount = $_POST['dataNewCount'];


$sql = "SELECT bid,ask,curr_pair FROM wp_ticker WHERE wp_ticker.curr_pair = '$dataNewCount' LIMIT 1";

$result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
            While ($row = mysqli_fetch_assoc($result)){
                    $val1 = $row['bid'];
                }
 }
 
mysqli_free_result($result);
mysqli_close($conn);
$dataNewCountfirst3 = substr($dataNewCount, -3, 3);
if ($dataNewCountfirst3 !== "EUR"){
    $geteuro = $dataNewCountfirst3."EUR";
}


$sql = "SELECT bid,ask,curr_pair FROM wp_ticker WHERE wp_ticker.curr_pair = '$geteur' LIMIT 1";;
$result = mysqli_query($conn1, $sql);
if (mysqli_num_rows($result) > 0){
    //echo $result2->bid;
    while($row = mysql_fetch_assoc($result)){
        $val2 =$row['bid'];
        

    }
        
} 

$data = ["$val1", "$val2"];
echo json_encode($data);

When i remove the second query, it works fine, but i need to run it because depending on the result of the first query, i need some more info from the same database table.

</div>

You are going about this all wrong, why do you have two connections? You can use a single connection to run multiple queries.

<?php
$server = "localhost";
$username ="XXXXX";
$password = "XXXXX";
$database = "XXXX";

$conn = mysqli_connect($server,$username,$password,$database);
$dataNewCount = $_POST['dataNewCount'];


$sql = "SELECT bid,ask,curr_pair FROM wp_ticker WHERE wp_ticker.curr_pair = '$dataNewCount' LIMIT 1";

$result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
      while ($row = mysqli_fetch_assoc($result)){
          $val1 = $row['bid'];
        }
}

mysqli_free_result($result);

$dataNewCountfirst3 = substr($dataNewCount, -3, 3);
if ($dataNewCountfirst3 !== "EUR"){
  $geteuro = $dataNewCountfirst3."EUR";
}


$sql = "SELECT bid,ask,curr_pair FROM wp_ticker WHERE wp_ticker.curr_pair = '$geteur' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0){
  //echo $result2->bid;
  while($row = mysql_fetch_assoc($result)){
    $val2 =$row['bid'];
  }
}

mysqli_close($conn); 

$data = ["$val1", "$val2"];
echo json_encode($data);
  • You also had an extra ; at the end of your second $sql.
  • You should use mysqli_error to debug your mysqli code.
  • You had a capitalised the 'w' in while (not an error but you should remain consistent).

I really think you should read up on the basics of mysqli.

Note: Requests can be spoofed. As a result I think you should validate your input data.