我正在使用 AJAX 使用 Php 从 MSSQL server 2017 检索数据,并在文本框中显示这些值。一个 MSSQL 查询提供,但其他查询不提供。
在 data.php 中,当 $stmt
等于 SELECT rmtype FROM rmmaster
时,数据正在加载到 AJAX.php 中的文本框 但当 $stmt
等于 SELECT * FROM rmmaster where rmnumber='102'
时,它没有值。但是,如果在 sql managment studio 中同时运行这两个查询,两者都可以正常工作并产生结果。
连接没有错误,只是这对一个人有效,对另一个无效。
This is AJAX.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<button type="button" id="element" class="element">Button</button>
<input type="text" class="form-control" id="usr">
</div>
</body>
<script type="application/javascript">
$('#element').on('click', function(e){
$.ajax({
url: 'data.php',
type: 'GET',
success: function(data){
alert(data);
$('#usr').val(data);
}
});
});
</script>
</html>
This is DATA.php
include_once ('home.php');
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$name="102";
$stmt = $conn->query("SELECT * FROM rmmaster where rmnumber='$name'"); // this is not giving results
//$stmt = $conn->query("SELECT rmtype FROM rmmaster"); // this is giving answers
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
if( $stmt->fetch() === false) {
die( print_r( sqlsrv_errors(), true));
}
while ($row = $stmt->fetch()){
echo ($row['rmtype'].$name);
}
home.php是到sql server的数据连接,那个文件没有错误。
最佳答案
您的代码总是读取第一个结果行,然后什么也不做,基本上是忽略它。
这个If从结果集中读取第一行
if( $stmt->fetch() === false) {
die( print_r( sqlsrv_errors(), true));
}
所以删除那个 IF ,当你只选择一行时它会起作用,当你选择多行时它也会返回所有行
include_once ('home.php');
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$name="102";
$stmt = $conn->query("SELECT * FROM rmmaster where rmnumber='$name'"); // this is not giving results
//$stmt = $conn->query("SELECT rmtype FROM rmmaster"); // this is giving answers
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
/*
if( $stmt->fetch() === false) {
die( print_r( sqlsrv_errors(), true));
}
*/
while ($row = $stmt->fetch()){
echo ($row['rmtype'].$name);
}
关于javascript - 通过 Php 通过 ajax 从 MSSQL 服务器获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106498/