How to use PHP to connect to sql server

前端 未结 14 1967
醉梦人生
醉梦人生 2020-11-30 11:37

I want to use PHP to connect to sql server database.

I installed xampp 1.7.0(php 5.2) and SQLSRV20. I\'ve added the extensions in php.ini and I get thi

相关标签:
14条回答
  • 2020-11-30 11:56

    MS SQL connect to php

    1. Install the drive from microsoft link https://www.microsoft.com/en-us/download/details.aspx?id=20098

    2. After install you will get some files. Store it in your system temp folder

    3. Check your php version , thread or non thread , and window bit - 32 or 64 (Thread or non thread , this is get you by phpinfo() )

    4. According to your system & xampp configration (php version and all ) copy 2 files (php_sqlsrv & php_pdo_sqlsrv) to xampp/php/ext folder .

    5. Add to php.ini file extension=php_sqlsrv_72_ts_x64 (php_sqlsrv_72_ts_x64.dll is your file which your copy in 4th step ) extension=php_pdo_sqlsrv_72_ts_x64 (php_pdo_sqlsrv_72_ts_x64.dll is your file which your copy in 4th step )

    6. Next Php Code

      $serverName ="DESKTOP-me\MSSQLSERVER01"; (servername\instanceName)

      // Since UID and PWD are not specified in the $connectionInfo array, // The connection will be attempted using Windows Authentication.

      $connectionInfo = array("Database"=>"testdb","CharacterSet"=>"UTF-8");

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

      if( $conn ) { //echo "Connection established.
      "; }else{ echo "Connection could not be established.
      "; die( print_r( sqlsrv_errors(), true)); }

      //$sql = "INSERT INTO dbo.master ('name') VALUES ('test')"; $sql = "SELECT * FROM dbo.master"; $stmt = sqlsrv_query( $conn, $sql );

      while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) { echo $row['id'].", ".$row['name']."
      "; }

      sqlsrv_free_stmt( $stmt);

    0 讨论(0)
  • 2020-11-30 12:00
    1. first download below software https://www.microsoft.com/en-us/download/details.aspx?id=30679 - need to install

    https://www.microsoft.com/en-us/download/details.aspx?id=20098 - when you run this software . it will extract dll file. and paste two dll file(php_pdo_sqlsrv_55_ts.dll,extension=php_sqlsrv_55_ts.dll) this location C:\wamp\bin\php\php5.6.40\ext\ (pls make sure your current version)

    2)edit php.ini file add below line extension=php_pdo_sqlsrv_55_ts.dll extension=php_sqlsrv_55_ts.dll

    Please refer screenshort add dll in your php.ini file

    0 讨论(0)
  • 2020-11-30 12:03

    Try this code

    $serverName = "serverName\sqlexpress"; //serverName\instanceName
    $connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    0 讨论(0)
  • 2020-11-30 12:04

    enable mssql in php.ini

    ;extension=php_mssql.dll

    to

    extension=php_mssql.dll

    0 讨论(0)
  • 2020-11-30 12:05

    if your using sqlsrv_connect you have to download and install MS sql driver for your php. download it here http://www.microsoft.com/en-us/download/details.aspx?id=20098 extract it to your php folder or ext in xampp folder then add this on the end of the line in your php.ini file

    extension=php_pdo_sqlsrv_55_ts.dll
    extension=php_sqlsrv_55_ts.dll
    

    im using xampp version 5.5 so its name php_pdo_sqlsrv_55_ts.dll & php_sqlsrv_55_ts.dll

    if you are using xampp version 5.5 dll files is not included in the link...hope it helps

    0 讨论(0)
  • 2020-11-30 12:06

    Use localhost instead of your IP address.

    e.g,

    $myServer = "localhost";
    

    And also double check your mysql username and password.

    0 讨论(0)
提交回复
热议问题