从SQL Server获取数据并以json格式制作

时间:2023-02-07 23:47:52

I'm trying to make a UI that gets its data from a SQL Server database. I want to get it through PHP. Currently, I'm able to make database connection and insert the data through the following code

我正在尝试创建一个从SQL Server数据库获取数据的UI。我想通过PHP获得它。目前,我可以通过以下代码建立数据库连接并插入数据

<?php
  
$serverName = "esdapocnv01";
$connectionInfo = array( 'Database'=>'TRG_TEST', 'UID'=>'testuser', 'PWD'=>'Towing@2');

$conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
	
	echo "Connection could not be established.<br />";
	
}



$class = $_POST['number'];
$teachers=$_POST['teachers1'];
$rooms=$_POST['rooms1'];

$sql = "insert into school(class,teachers,rooms) values ('$class','$teachers','$rooms') ";



$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo "<option value='" . $row['station'] . "'>" . $row['station'] . "</option>";
                               
}
sqlsrv_free_stmt( $stmt);
?>

Now I want to get data from the database table and convert it into JSON format so that I can use it in the UI

现在我想从数据库表中获取数据并将其转换为JSON格式,以便我可以在UI中使用它

2 个解决方案

#1


0  

<?php
    //fetch table rows from mysql db
    $sql = "select * from tbl_employee";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $emparray = array();
    while($row =mysqli_fetch_assoc($result)){
       $a['class']=$row['class'];
       $a['teacher']=$row['teachers'];
       $a['room']=$row['rooms'];
       $a['addedat']=Now(); //example of adding extra data if required
       $b[]=$a;
    }
    echo json_encode($b);
?>

#2


0  

$.ajax({
    url: "data1.php", 
    data: {"id": "EMP001"},
    success: function(result){

        var obj = jQuery.parseJSON( result );
        alert( obj.name );

    }
});

data1.php

<?php
    $id=$_REQUEST['id'];
    //fetch table rows from mysql db
    $sql = "SELECT * FROM `tbl_employee` WHERE `id`='$id'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $emparray = array();
    while($row =mysqli_fetch_assoc($result)){
       $emparray[] = $row;
    }
    echo json_encode($emparray);
?>

#1


0  

<?php
    //fetch table rows from mysql db
    $sql = "select * from tbl_employee";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $emparray = array();
    while($row =mysqli_fetch_assoc($result)){
       $a['class']=$row['class'];
       $a['teacher']=$row['teachers'];
       $a['room']=$row['rooms'];
       $a['addedat']=Now(); //example of adding extra data if required
       $b[]=$a;
    }
    echo json_encode($b);
?>

#2


0  

$.ajax({
    url: "data1.php", 
    data: {"id": "EMP001"},
    success: function(result){

        var obj = jQuery.parseJSON( result );
        alert( obj.name );

    }
});

data1.php

<?php
    $id=$_REQUEST['id'];
    //fetch table rows from mysql db
    $sql = "SELECT * FROM `tbl_employee` WHERE `id`='$id'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $emparray = array();
    while($row =mysqli_fetch_assoc($result)){
       $emparray[] = $row;
    }
    echo json_encode($emparray);
?>