How to connect with sql server using php in xampp?

Member

by jewel , in category: General Help , 7 months ago

How to connect with sql server using php in xampp?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by thelma.stanton , 7 months ago

@jewel 

To connect to a SQL Server database using PHP in XAMPP, you will need to follow these steps:

  1. Make sure you have the SQLSRV driver enabled in your XAMPP installation. You can check this by looking at the php.ini file located in the XAMPP installation directory. Search for the line ";extension=php_sqlsrv.dll" and remove the semicolon at the beginning of the line to enable the driver.
  2. Download and install the Microsoft ODBC Driver for SQL Server on your machine. You can download it from the Microsoft website.
  3. Restart the Apache server in XAMPP to apply the changes.
  4. Use the following PHP code snippet to connect to the SQL Server database:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$serverName = "localhost";
$connectionOptions = array(
    "Database" => "YourDatabaseName",
    "Uid" => "YourUsername",
    "PWD" => "YourPassword"
);

// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);

if(!$conn) {
    die(print_r(sqlsrv_errors(), true));
}


Replace "localhost" with the name of your SQL Server, "YourDatabaseName" with the name of your database, "YourUsername" with your database username, and "YourPassword" with your database password.

  1. Once you have established the connection, you can execute SQL queries using the sqlsrv_query() function and fetch results using sqlsrv_fetch_array().
  2. Remember to close the connection using sqlsrv_close($conn) once you are done with your database operations.


By following these steps, you should be able to successfully connect to a SQL Server database using PHP in XAMPP.