繁体   English   中英

通过Php从ajax获取MSSQL服务器中的数据

[英]fetch data from MSSQL server by ajax through Php

我使用AJAX使用Php从MSSQL服务器2017检索数据,并在文本框中显示这些值。 一个MSSQL查询正在给出但其他人没有。 data.php中,当这里的$stmt等于SELECT rmtype FROM rmmaster ,数据加载到AJAX.php中的文本框,但是当这里的$stmt等于SELECT * FROM rmmaster where rmnumber='102'这里没有值。 但是如果在sql管理工作室中运行这两个查询,两者都在工作并且有结果。

连接没有错误,只是这个工作一个而不是其他工作。

这是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> 

这是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);   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM