Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to sql server using PHP7? (What am I missing?)

Here's the phpinfo output: version.php

Here's the code:

$serverName = "X.X.X.X";
$connection = array( "UID"=>"UserID", "PWD"=>"Password123", "Database"=>"database_name");
$conn = sqlsrv_connect( $serverName, $connection);

if ($conn === false) {
    $myfile3 = fopen("log.txt", "w");
    fwrite($myfile3, sqlsrv_errors());
    fclose($myfile3);

};

$tsql = "SELECT top 10 pName from products";
$stmt = sqlsrv_query( $conn, $tsql);
$row = sqlsrv_fetch_array($stmt);

$myfile4 = fopen("log.txt", "w");
fwrite($myfile4,  $row[0]);
fclose($myfile4);
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);

Nothing is written to the log file. Even if I hard code text in the fwrite($myfile3, "hard coded text"); place, nothing is written out.

Here's the extensions section in the php.ini file

[ExtensionList]
;extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_curl.dll
extension=php_exif.dll
extension=php_xmlrpc.dll
extension=php_openssl.dll
extension=php_soap.dll
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_imap.dll
extension=php_tidy.dll
extension=php_sqlsrv_7_nts_x64.dll
;extension=php_sqlsrv_7_ts_x64.dll

Lastly, I know I don't need all of these, but these are 4 dlls I have in the ext folder.

php_sqlsrv_7_nts_x64.dll
php_sqlsrv_7_nts_x86.dll
php_sqlsrv_7_ts_x64.dll
php_sqlsrv_7_ts_x86.dll
like image 479
Mark Avatar asked Nov 07 '16 00:11

Mark


People also ask

How do I connect to a SQL Server database?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).

How do I enable SQL connection?

Security & ConnectionsRight-click on your server name and click 'Properties'. Go to the Security page for Server Authentication, and select 'SQL Server and Windows Authentication' mode. Then, go to the Connections page and ensure that "Allow remote connections to this server" is checked, and click OK.

How do I connect to a SQL Server name?

In Server name, type or select the name of your SQL server instance in the form <hostname>\<SQL_instance_name>. Example: On a server named MySQLserver, when using SQL Express with the default instance name, enter MySQLserver\sqlexpress. In Authentication, select Windows Authentication. Click Connect.


2 Answers

Using pdo:

$serverName = "(local)\sqlexpress";  

/* Connect using Windows Authentication. */  
try  
{  
  $conn = new PDO( "sqlsrv:server=$serverName ; Database=AdventureWorks", "", "");  
  $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
}  
catch(Exception $e)  
{   
  die( print_r( $e->getMessage() ) );   
} 

Procedural way:

$serverName = "(local)\sqlexpress";  
$connectionOptions = array("Database"=>"AdventureWorks");  

/* Connect using Windows Authentication. */  
$conn = sqlsrv_connect( $serverName, $connectionOptions);  
if( $conn === false )  
die(sqlsrv_errors());  

Click here for More Information

like image 64
viral barot Avatar answered Sep 21 '22 19:09

viral barot


First you need to isolate your problem - is this something wrong with your file writing or something wrong with connecting to SQL server, or something wrong with your PHP setup?

Troubleshooting SQL Server

You may wish to use the SQLSRV_ERR_ALL flag when calling sqlsrv_errors() for as much detail as possible, and use a die() or simply print_r/echo the result to the screen (instead of writing to a file) so you can see if there's something wrong there - have a look at the examples on the sqlsrv_connect documentation page.

Next I would try to make sure that you're specifying the port correctly, e.g.:

$serverName = 'X.X.X.X, 1433';

Or specify the SQL Server instance, e.g.:

$serverName = 'X.X.X.X\sqlexpress';

Also make sure that your user, password and database name are all correct, and that the user you've specified has the correct permissions on that database. And of course that it's running on port 1433 (or change the above to whatever port you have it running on).

After that, I would consider adding these properties prior to your sqlsrv_connect() call to elevate as much as possible into errors coming back for troubleshooting the connection:

sqlsrv_configure('WarningsReturnAsErrors', true);
sqlsrv_configure('LogSubsystems', SQLSRV_LOG_SYSTEM_ALL);
sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL);

See if that turns up anything more useful from SQL server directly.

Troubleshooting file-writing

  • you need to have a look at your fopen - you can use a mode instead of w to make sure that the file isn't truncated when you open it
  • You should also check to see whether or not the returned $myfile3 is false as per the documentation, in case the file open itself is failing.
    • If it is, then you may have a permissions problem in the directory that you're trying to create the file. If the file is already created ahead of time for you (no need to worry about creating it) you also may not have write permissions on that file. If the open is failing it should generate an E_WARNING (which should display on the page since your error_reporting configuration is set to E_ALL, but you could also check the PHP error logfile). You can also use file_exists and is_readable() to help check permissions.
    • When you say hard-coding a write value doesn't do any writes, this leads me to think that this is the actual problem.
  • I would also consider using different log files for each part (not log.txt in both cases)

Troubleshooting your PHP

Have a look at the Microsoft documentation to ensure that your DLLs match the right PHP7 dll you're using on your version of Windows, although that doesn't specify the Operating system requirements for v4.0 of the driver: they may not support Windows server 2008, although I would be surprised. They also don't show which versions of SQL server they support connecting to, so if you're on an old version of SQL server that might be a problem as well.

like image 24
Leith Avatar answered Sep 22 '22 19:09

Leith