14

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 came:

$conn_array = array (
    "UID" => "sa",
    "PWD" => "root",
    "Database" => "nih_bw",
);
$conn = sqlsrv_connect('BILAL', $conn_array);
if ($conn){
    echo "connected";
    $result = sqlsrv_query($db->db_conn,"SELECT * FROM routines");
}else{
    die(print_r(sqlsrv_errors(), true));
}
sqlsrv_close($conn);
?>
Mureinik
  • 297,002
  • 52
  • 306
  • 350
user3185936
  • 1,567
  • 6
  • 23
  • 44

4 Answers4

15

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));
}
Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
Quijote Shin
  • 501
  • 4
  • 11
2

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 />";
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I get this error: https://www.dropbox.com/s/lod1s8olf1iwog3/errors.png, its in the $result line it errors. – user3185936 Apr 17 '14 at 11:19
  • @user3185936 Why are you using `$db->db_conn`? You set connection to `$conn` with `sqlsrv_connect()` so use it: `$result = sqlsrv_query($conn, "SELECT * FROM routines");` – alpakyol Apr 19 '14 at 08:02
0

Try this:

while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
  var_dump($row);
}

sqlsrv_free_stmt($result);
Greg Motyl
  • 2,478
  • 17
  • 31
  • 3
    When you answer with code, please provide a brief explanation along with the code. – Thom Apr 17 '14 at 11:41
0
$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 />';
        }
  • This does not provide an answer to the question. You can [search for similar questions](https://stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](https://stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](https://stackoverflow.com/tour) – phoenixstudio Feb 12 '21 at 20:33