Search code examples
phpsql-serversqlsrv

multiple values an array php - Mssql


I'm pulling data from mssql. The data I have captured is more than one. It only records one data to Array. As you can see in the photo, there is more than one content with soru_tur = 1. How can I save soru_baslik and soru_icerik data to array? I show the data I have captured in my swift application. A database screenshot has been added with the var_dump output.

" json["soru_baslik"] " >> I want to print it out from swift. The screenshot of the output is as follows

            <?php
          ...
            $conn = sqlsrv_connect( $serverName, $connectionInfo );
            if( $conn === false ) {
                die( print_r( sqlsrv_errors(), true));
            }
            $json = file_get_contents('php://input');    
            $sql = "SELECT soru_baslik, soru_icerik FROM ... WHERE soru_tur = 1";
            $stmt = sqlsrv_query( $conn, $sql );
            if( $stmt === false) {
                die( print_r( sqlsrv_errors(), true) );
            }
        $soruArray = array();
        $soruicerikArray = array();
      $array = array();


   while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
     $results[] = Array("soru_baslik" => $row['soru_baslik'], "soru_icerik" => $row['soru_icerik']);
   }
    //var_dump($array);

   echo '<pre>'; print_r($results); echo '</pre>';
    sqlsrv_free_stmt( $stmt);
            ?>

database new output


Solution

  • If I understand your question correctly, one issue with your code is that you are initializing the $result variable on each iteration. You need to initialize this variable once and then append items on each iteration. You may try with the following approaches:

    Example 1 (fetch only two specific columns):

    <?php
        $conn = sqlsrv_connect( $serverName, $connectionInfo );
        if( $conn === false ) {
            die( print_r( sqlsrv_errors(), true));
        }
        $json = file_get_contents('php://input');    
        $sql = "SELECT soru_baslik, soru_icerik FROM ... WHERE soru_no = 1";
        $stmt = sqlsrv_query($conn, $sql);
        if ($stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
    
        $results = array();
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $results[] = array(
                "..._baslik" => $row['..._baslik'], 
                "s..._icerik" => $row['..._icerik']
            );
        }
    
        header("Content-Type: application/json");
        echo json_encode($results);
    
        sqlsrv_free_stmt( $stmt);
    ?>
    

    Example 2 (fetch all columns):

    <?php
        $conn = sqlsrv_connect( $serverName, $connectionInfo );
        if( $conn === false ) {
            die( print_r( sqlsrv_errors(), true));
        }
        $json = file_get_contents('php://input');    
        $sql = "SELECT soru_baslik, soru_icerik FROM ... WHERE soru_no = 1";
        $stmt = sqlsrv_query($conn, $sql);
        if ($stmt === false) {
            die( print_r( sqlsrv_errors(), true) );
        }
    
        $results = array();
        while ($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $a = array();
            foreach($row as $column => $value) {
                $a[$column] = $value;   
            }   
            $results[] = $a;
        }
    
        header("Content-Type: application/json");
        echo json_encode($results);
    
        sqlsrv_free_stmt( $stmt);
    ?>