我正在尝试从Mysql数据库中检索一些数据并将结果放在一个表单中。
当我查询数据库一次并返回结果时,它工作正常。 但是当我使用结果运行另一个查询然后尝试返回结果时我得到500错误?
脚本及其load-data.php文件如下所示:
<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);
当我删除第二个查询时,它工作正常,但我需要运行它,因为根据第一个查询的结果,我需要来自同一个数据库表的更多信息。
答案 0 :(得分:2)
你这一切都错了,为什么你有两个联系?您可以使用单个连接来运行多个查询。
<?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);
;
结束时还有一个额外的$sql
。mysqli_error
调试mysqli代码。while
中大写了'w'(不是错误,但你应该保持一致)。我真的认为你应该阅读mysqli
的基础知识。
注意:请求可能是欺骗性的。因此,我认为您应该验证您的输入数据。