Advertisement
stevennathaniel

PHP MS SQL SQLSRV : koneksi

Jun 17th, 2021
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2.  
  3.     /*
  4.    
  5.         Test koneksi ke MS SQL Server
  6.    
  7.    
  8.     */
  9.    
  10.    
  11.     /* specify the server and connection string attributes */
  12.    
  13.     $serverName = "win7\SQLEXPRESS";
  14.    
  15.     $connectionInfo = array("Database"=>"latihan");
  16.    
  17.    
  18.     /* Connect using Windows Authentication */
  19.    
  20.     $con = sqlsrv_connect($serverName, $connectionInfo);
  21.    
  22.     if($con == false){
  23.        
  24.        
  25.         echo "Unable to connect.</br>";
  26.        
  27.         die(print_r(sqlsrv_errors(), true));
  28.     }
  29.    
  30. /* Query SQL Server for the login of the user accessing the database. */
  31.  
  32.  
  33. $tsql = "select * from pegawai";
  34.  
  35. $stmt = sqlsrv_query($con, $tsql);
  36.  
  37. if($stmt === false){
  38.    
  39.    
  40.     echo "Error in executing query.</br>";
  41.    
  42.     die(print_r(sqlsrv_errors(), true));
  43. }
  44.  
  45.  
  46. /* Retrieve and display the results of the query */
  47.  
  48. $row = sqlsrv_fetch_array($stmt);
  49.  
  50. echo "ID Pegawai : ".$row[0]."</br>";
  51.  
  52. echo "Nama Pegawai : ".$row[1]."</br>";
  53.  
  54. echo "Alamat Pegawai : ".$row[2]."</br>";
  55.  
  56.  
  57.  
  58. /* Free statement and connection resources. */
  59.  
  60.  
  61. sqlsrv_free_stmt($stmt);
  62.  
  63. sqlsrv_close($con);
  64.  
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement