Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch data from MSSQL server by ajax through Php

I am using AJAX to retrieve data from MSSQL server 2017 using Php and show those values in a text box. One MSSQL query is giving but other doesn't. In data.php when $stmt here is equals to SELECT rmtype FROM rmmaster, data is loading to text box in AJAX.php but when $stmt here is equals to SELECT * FROM rmmaster where rmnumber='102' this doesn't have values. But if run both the queries in sql managment studio, both are working and have results.

No error in connections, Just this working to one and not to other.

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 is the data connection to the sql server and no error in that file.

like image 950
S.Hettz Avatar asked Oct 17 '22 08:10

S.Hettz


1 Answers

Your code is always reading the first result row and then doing nothing with it, basically ignoring it.

This If reads the first row from the result set

if( $stmt->fetch() === false) {
    die( print_r( sqlsrv_errors(), true));
}

So remove that IF and it will work when you are only selecting one row and also will return all the rows when you are selecting more than one row

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);   
}
like image 116
RiggsFolly Avatar answered Oct 21 '22 20:10

RiggsFolly