Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get mssql_connect to work using PHP7?

Tags:

php

ubuntu

php-7

While porting a web app to a server using PHP7, I encounter one problem: I can't get mssql_connect to work. I found out that mssql doesn't work yet (or will never work) on PHP7.

What's the quickest way to connect to MSSQL using PHP7 on Ubuntu (Nginx, php-fpm)?

like image 652
Paul Avatar asked Jan 13 '16 13:01

Paul


People also ask

How connect MS SQL with PHP?

Running MS SQL Queries from PHP See the full list of MS SQL functions in PHP at php.net. The following script is an example of an MS SQL SELECT query run from PHP. <? php $query ="SELECT col1,col2 FROM tablename"; $result =mssql_query($query); while ( $record = mssql_fetch_array($result) ) { echo $record["col1"] .

Can PHP work with SQL Server?

The Microsoft Drivers for PHP for SQL Server enable integration with SQL Server for PHP applications. The drivers are PHP extensions that allow the reading and writing of SQL Server data from within PHP scripts.

What is sqlsrv_ connect?

sqlsrv_connect — Opens a connection to a Microsoft SQL Server database.


1 Answers

The connection function is not mssql_connect() anymore. Since php 5.3 it's been deprecated. Now on php 7 this old function vanishes. But don't worry ;) Nowadays you can use the sqlsrv_connect() function instead.

Keep in mind that in the new method you need to set up the parameters properly. There are some differences. Here comes a little example.

<?php
$serverName = "serverName\sqlexpress, 1542"; //serverName\instanceName, portNumber (1433 by default)
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Successfuly connected.<br />";
}else{
     echo "Connection error.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

May be this answer comes a bit late for you but I hope it is not too late for someone.

like image 102
Jorgeska Avatar answered Sep 28 '22 01:09

Jorgeska