Prerequesit installs
- Established a new htdocs directory and host in Xampp and added a new host in local host config
- Installed SSMS
- Installed SQL Server 2019 config manager
Installing PHP Extensions
- Downloaded and added PDO extension for PHP on Windows ( Click Download Microsoft Drivers for PHP for SQL Server (Windows))
In my case this meant extracting and moving the .dll toC:\xampp\php\ext
I used php_pdo_sqlsrv_73_nts_x64.dll, 73 being the PHP version (found in phpinfo()) x64 being the architechture
- edit the php.ini in
C:\xampp\php\php.ini
, checked this was the only ini config loaded
- Restart Apache
- Checked SQLSRV was present in phpinfo()
Importing MSSQL DB .bak
- Run SSMS as administrator
- In object explorer choose Connect > Database Engine, choose your local server probably COMPUTERNAME\SQLEXPRESS and in Authentication choose Windows Authentication
- Follow instructions on how to import the .bak
- You might start running into permissions issues here preventing you from importing, this is because the Windows Auth user might not have the correct access rights.
Getting Acess to DB
- Run SQL Server 2019 config manager as Administrator
- In SQL Server Services right click your SQL Server instance and click Properties in Startup Parameters add “-m” and click Add, Apply, Ok,
- Right click the SQL Server instance again and restart it
- Restart SSMS (run as administrator) and connect as your Windows user again
- Now you are in single admin mode and can do more!
- In Object Explorer open folders Security > Logins
- Right Click your own user and in Server Roles, go wild choose what you like
- This gives you permission to create additional logins. Create a new login, using SQL Server Authentication, set the Server Roles etc as appropriate
Reference Tutorial - Now Repeat step 2 except remove the -m
- Restart everything, Services, SSMS etc
Connecting with PHP PDO and writing first query
Using PHP:sqlsrv_connect threw errors
Call to undefined function sqlsrv_connect()
So I used PDO Object instead Reference Tutorial
<?php $db = new PDO("sqlsrv:Server=COMPUTERNAME\SQLEXPRESS;Database=DBNAME", "USERNAME", "PASSWORD"); $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $db->setAttribute( PDO::SQLSRV_ATTR_QUERY_TIMEOUT, 1 ); ?>
and begin with the first query “list all table names in the database”
$query = "SELECT * FROM SYSOBJECTS WHERE xtype = 'U'"; $test = $db->query( $query ); while ( $row = $test->fetch( PDO::FETCH_ASSOC ) ){ print_r( $row['name'] ."\n" ); }
Was this article helpful?
YesNo