add_action('wp_footer', function () { echo ''; }, 99);
add_action('wp_footer', function () { echo ''; }, 99);
The post How to connect to MS SQL Server using PHP appeared first on Coding Sips.
]]>For windows operating systems you can download required dlls from the following link
https://www.microsoft.com/en-us/download/details.aspx?id=20098
for PHP 7.4 use the following Dlls
for PHP 5.2 > download sqlsrv40.exe from above link.
Run this exe it will ask for folder location to extract dll file.
after completing goto that folder and copy required dlls to php extension folder.
if you are using php 5.3 copy 4 dll files ending with 54dll
if you are using php 5.6 copy 4 dll files ending with 56dll
and paste in PHP ext folder for me it is C:\wamp64\bin\php\php5.6.35\ext
I have copied the following files
Goto php.ini file and add the following lines Better to add after other extension entries
Extension=php_sqlsrv_56_ts.dll
Extension=php_sqlsrv_56_nts.dll
Extension=php_pdo_sqlsrv_56_ts.dll
Extension=php_pdo_sqlsrv_56_nts.dll
If MS Sql Server is installed on the same PC where PHP is installed than this is not required, but if the PC where PHP is installed has no MS SQL server than it will need MS-SQL-SERVER-CLI to operate sql server related commands. for windows operating systems you can download it from the following link, and its installation is easy just few clicks.
https://download.microsoft.com/download/B/E/D/BED73AAC-3C8A-43F5-AF4F-EB4FEA6C8F3A/ENU/x64/sqlncli.msi
Restart WAMP server, (All Services). it will be better to exit wamp and restart again. For me it was not working with simple restating all services
After restarting we are all done.
TO connect to MS SQL Server create a PHP file and pate the following code, and replace ip, DatabaseName, UserName and YourPassword
$cinfo = array(
"Database" => 'employees_database',
"UID" => '192.168.11.12',
"PWD" => 'my_password'
);
$conn = sqlsrv_connect(v18.v15,$cinfo);
if( $conn === false )
{
echo "ERROR: (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql = "select top 10 * from employees";
$stmt = sqlsrv_query($conn, $sql );
if( $stmt === false ) {
return "ERROR (sqlsrv_query): ".debug(sqlsrv_errors());
}
echo '';
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo '';
echo '' . $row['employee_name'] . ' ';
echo '' . $row['employee_email'] . ' ';
echo ' ';
}
echo '
';
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
Here I will put code snippet for cakephp to MSSql-Server connection also
/*
connect to MS SQL Server using Cake PHP
tested with cakephp 2.x using php5 and 7
by Alam
*/
public $sqlsrv = array(
'datasource' => 'Database/Sqlserver',
'persistent' => false,
'host' => '192.168.0.2',
'login' => 'UserName',
'password' => 'YourPassword',
'database' => 'DatabaseName',
'prefix' => '',
//'encoding' => 'utf8',
);
this is all done, hope this was helpful, write you experience in comments below.
The post How to connect to MS SQL Server using PHP appeared first on Coding Sips.
]]>