How to use PHP to connect to sql server

I want to use PHP to connect to a SQL Server database.

I installed xampp 1.7.0 (php 5.2) and SQLSRV20. I added extensions in php.ini and I get this error:

 Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 10.85.80.229 in C:\xampp\htdocs\xampp\test.php on line 07 

code:

 <?php $myServer = "10.85.80.229"; $myUser = "root"; $myPass = "pass"; $myDB = "testdb"; $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); ?> 

What does this error message mean and how can I connect to SQL Server?

+6
source share
11 answers

enable mssql in php.ini

;extension=php_mssql.dll

to

extension=php_mssql.dll

+6
source
 <?php $serverName = "ServerName"; $uid = "sqlusername"; $pwd = "sqlpassword"; $databaseName = "DBName"; $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName); /* Connect using SQL Server Authentication. */ $conn = sqlsrv_connect( $serverName, $connectionInfo); $tsql = "SELECT id, FirstName, LastName, Email FROM tblContact"; /* Execute the query. */ $stmt = sqlsrv_query( $conn, $tsql); if ( $stmt ) { echo "Statement executed.<br>\n"; } else { echo "Error in statement execution.\n"; die( print_r( sqlsrv_errors(), true)); } /* Iterate through the result set printing a row of data upon each iteration.*/ while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC)) { echo "Col1: ".$row[0]."\n"; echo "Col2: ".$row[1]."\n"; echo "Col3: ".$row[2]."<br>\n"; echo "-----------------<br>\n"; } /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?> 

http://robsphp.blogspot.ae/2012/09/how-to-install-microsofts-sql-server.html

+3
source

Try this code

 $serverName = "serverName\sqlexpress"; //serverName\instanceName $connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password"); $conn = sqlsrv_connect( $serverName, $connectionInfo); 
+1
source

if you use sqlsrv_connect, you need to download and install the MS SQL driver for your php. download here http://www.microsoft.com/en-us/download/details.aspx?id=20098 extract it to the php folder or to the xampp folder then add this to the end of the line in the 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 is php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll

If you use xllp files of version 5.5 dll, they are not included in the link ... hope this helps

+1
source

Use localhost instead of your IP address.

eg,

 $myServer = "localhost"; 

And also double check your mysql username and password.

0
source

for further investigation: print the mssql error message:

 $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message()); 

It is also important to specify the port: on MS SQL Server 2000, separate it with a comma:

 $myServer = "10.85.80.229:1443"; 

or

 $myServer = "10.85.80.229,1443"; 
0
source

For the following code, you need to enable mssql in php.ini, as described at this link: http://www.php.net/manual/en/mssql.installation.php

 $myServer = "10.85.80.229"; $myUser = "root"; $myPass = "pass"; $myDB = "testdb"; $conn = mssql_connect($myServer,$myUser,$myPass); if (!$conn) { die('Not connected : ' . mssql_get_last_message()); } $db_selected = mssql_select_db($myDB, $conn); if (!$db_selected) { die ('Can\'t use db : ' . mssql_get_last_message()); } 
0
source

I had the same problem (well, I hope the same). In any case, it turned out to be my version of ntwdblib.dll, which is deprecated in my PHP folder.

http://dba.fyicenter.com/faq/sql_server_2/Finding_ntwdblib_dll_Version_2000_80_194_0.html

0
source
 $dbhandle = sqlsrv_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer"); 

Hope this helps.

0
source
  $server_name = "your server name"; $database_name = "your database name"; try { $conn = new PDO("sqlsrv:Server=$server_name;Database=$database_name;ConnectionPooling=0", "user_name", "password"); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { $e->getMessage(); } 
0
source

Try the following:

 $servername = "10.85.80.229"; $username = "root"; $password = "pass"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; 
-3
source

Source: https://habr.com/ru/post/953225/


All Articles