PHP sqlsrv query to database

后端 未结 4 2162
独厮守ぢ
独厮守ぢ 2021-02-13 18:10

I am migrated from MySQL to MS SQL Server, and trying to fetch all data from the routines table. I am connected but unsure how to fetch data with sqlsrv. This is how far I have

相关标签:
4条回答
  • 2021-02-13 18:33
    $conn_array = array (
        "UID" => "sa",
        "PWD" => "root",
        "Database" => "nih_bw",
    );
    $conn = sqlsrv_connect('BILAL', $conn_array);
    if ($conn){
        echo "connected";
        if(($result = sqlsrv_query($conn,"SELECT * FROM routines")) !== false){
            while( $obj = sqlsrv_fetch_object( $result )) {
                  echo $obj->colName.'<br />';
            }
        }
    }else{
        die(print_r(sqlsrv_errors(), true));
    }
    

    that work great for me, but please any one can help me to display the result on a table ?

    I mean this parts :

    while( $obj = sqlsrv_fetch_object( $result )) {
                  echo $obj->colName.'<br />';
            }
    
    0 讨论(0)
  • 2021-02-13 18:36

    After you've successfully executed the query with sqlsrv_query you can fetch the results, e.g., by using sqlsrv_fetch_array:

    $result = sqlsrv_query($db->db_conn, "SELECT * FROM routines");
    if($result === false) {
        die( print_r( sqlsrv_errors(), true) );
    }
    
    while( $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC) ) {
        echo $row['column1'].", ".$row['column2']."<br />";
    }
    
    0 讨论(0)
  • 2021-02-13 18:49

    Try this:

    while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
      var_dump($row);
    }
    
    sqlsrv_free_stmt($result);
    
    0 讨论(0)
  • 2021-02-13 18:52

    First if I'm not wrong you are storing sqlsrv_connect result into $conn and this result isn't a class obj its a resource, so remove $db->conn

    This example, will connect, then fetch if there are resources returned from sqlsrv_query

    $conn_array = array (
        "UID" => "sa",
        "PWD" => "root",
        "Database" => "nih_bw",
    );
    $conn = sqlsrv_connect('BILAL', $conn_array);
    if ($conn){
        echo "connected";
        if(($result = sqlsrv_query($conn,"SELECT * FROM routines")) !== false){
            while( $obj = sqlsrv_fetch_object( $result )) {
                  echo $obj->colName.'<br />';
            }
        }
    }else{
        die(print_r(sqlsrv_errors(), true));
    }
    
    0 讨论(0)
提交回复
热议问题