Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Monday, 1 February 2010

How to connect to MSSQL (ODBC) from Linux PHP

Marcin Gil has an excellent guide about how to setup ODBC drivers on Linux to talk to a Microsoft MSSQl Server running on Windows. This uses the FreeTDS drivers & unixODBC, along with standard PHP/Apache setup.

After following these instructions, I found my database was still giving me the following error:

Warning: odbc_connect() [function.odbc-connect]: SQL error: [unixODBC][FreeTDS][SQL Server]Unable to connect to data source, SQL state S1000 in SQLConnect in /var/www/testdatabase/stdFunctions.php on line 49
Couldn't connect to SQL Server database


This was due to my database code not passing the correct username and password. I had to edit the odbc_connect() line to read as follows:

odbc_connect("database", "DOMAIN\username", "password");

Monday, 7 December 2009

FIX: PHP MSSQL odbc_num_rows() returns -1

Running MSSQL with PHP  on Windows sometimes returns -1 for the function odbc_num_rows() even when there are rows in the result. This function fixes the problem:

 function best_odbc_num_rows($result,$currentRow=0)  {
   $numRecords = 0;
  odbc_fetch_row($result, 0);
  while (odbc_fetch_row($result))
  {
    $numRecords++;
  }
  odbc_fetch_row($result, $currentRow);
  return $numRecords;
}