11

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
DForck42
  • 19,789
  • 13
  • 59
  • 84
Mark
  • 1,455
  • 3
  • 28
  • 51
  • Have you tried with `$serverName = "serverName\sqlexpress"; //serverName\instanceName` ? And without file writing I mean putting die like `die( print_r( sqlsrv_errors(), true));` ? – Rakesh Sojitra Nov 09 '16 at 05:32
  • Also make sure that SQL server is running on the default port (1433) otherwise you have to specify it in addition to the Ip address – Thomas G Nov 09 '16 at 13:55
  • What are the error/warnings? If nothing, be sure to have them turned on in .ini file. Try wrapping all in a `try/catch` for exceptions. Of course check SQL statement if returns anything. – Parfait Nov 09 '16 at 14:35
  • Have you tried to use PDO ? Have a look to http://php.net/manual/fr/ref.pdo-sqlsrv.php or https://github.com/Microsoft/msphpsql. – Weenesta - Mathieu Dormeval Nov 09 '16 at 16:31
  • Has the server run out of disk space? – UltrasoundJelly Nov 10 '16 at 19:36
  • 1
    You are writing errors to log.txt and then reopen log.txt with "w" option that replaces its content with $row[0] that is empty if $conn was false. So please use an "else" to "if ($conn === false)". Also you can open the file with "a" option. Or simply print errors on screen. – José Carlos PHP Nov 11 '16 at 02:59
  • @Mark Any luck with the debugging suggestions provided? – Leith Nov 15 '16 at 05:51
  • Can you check if the web server is 64 or 32 bit? – Rav Nov 15 '16 at 05:52
  • Can you try doing an `echo` for the error message instead of `fwrite`? Also, it would be great if you could create a temporary folder on your server somewhere and create a temporary database and share access to the particular folder and the database so that we can try conducting some experiments and help you eventually (if this is acceptable). – Jigarius Nov 15 '16 at 19:54

4 Answers4

10

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

viral barot
  • 631
  • 4
  • 8
  • 1
    OP doesn't include anything to indicate using PDO... also `FormatErrors` appears to be a custom function (lifted from comments in http://stackoverflow.com/questions/28468160/connecting-to-an-sql-server-database-with-php) and will cause an `undefined function` error if used. Also doesn't address anything to do with the file writing issues. – Leith Nov 16 '16 at 04:44
2

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.

Leith
  • 3,139
  • 1
  • 27
  • 38
1

As an alternative you could try to install PDO_SQLSRV driver instead. This is an example for connecting to SQL-Server using Windows authentication.

$serverName = "(local)\sqlexpress";    

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

For more information take a look at

Community
  • 1
  • 1
1

From the docs, this would give you more information: http://php.net/manual/en/function.sqlsrv-connect.php

$connectionInfo = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

If that doesn't give you an error maybe you can't write to the file due to permissions?

bstricks
  • 823
  • 8
  • 14