Archive for September, 2010

SQL Server Driver for PHP Setup Tutorial

Thursday, September 16th, 2010

So recently for work I had to work with some data in a Microsoft Access database. I imported this into SQL Server using the Microsoft Management Studio because the tools made my life easy. However my dev environment was already setup for php and I didn’t really feel like setting up an environment for ASP just to create some simple web pages for myself. I also wanted to avoid having to convert my db’s from Access to SQL Server and then import to MySQL. The answer is Microsoft’s opensource driver for PHP. This article will get you up and running in no time.

  1. Start by downloading Microsofts PHP Driver
  2. Extract it to a directory on your computer.
  3. Open a page that calls phpinfo() and look for the following information:
    • Version – 5.3 or 5.2 for example
    • Compiler – VC6 or VC9
    • Thread Safety – enabled or disabled
  4. Find the dll that in your extracted folder that matches the above information. For example sqlsrv_5.3_ts_vc6.dll would be for Version 5.3, Thread Safe Enabled and compiled in VC6.
  5. Copy the .dll into your php ext directory. For a WAMP installation this would typically be in c:\wamp\bin\php\phpversion\ext
  6. Enable the dll by either editing your php.ini file or from the WAMP taskbar icon
    • Click the taskbar icon -> restart all services  - the dll will show up in your extensions list
    • Click the taskbar icon again -> PHP -> PHP Extensions -> check php_sqlsrv_5.3_ts_vc6
    • Revisit your phpinfo() page and you should no have a section called sqlsrv
  7. Finally you just need to code up a page to grab some information from your sql database
$serverName = "(local)";
$connectionOptions = array("Database"=>"yoursqldb");

$conn = sqlsrv_connect( $serverName, $connectionOptions);

if( $conn === false )  { die( FormatErrors( sqlsrv_errors() ) ); } //terminate on failed connection

	$tsql = "SELECT TOP 100 [yourcolumn] FROM [yoursqldb]";
	$getRows = sqlsrv_query( $conn, $tsql);
	if ( $getRows === false) { die( FormatErrors( sqlsrv_errors() ) ); }

        //for each row output each column value
	while( $row = sqlsrv_fetch_array( $getRows, SQLSRV_FETCH_ASSOC)) {
		foreach ( $row as $key => $value ) {
			echo $value."<br />";
		}
	}

/* Free the statement and connection resources. */
sqlsrv_free_stmt( $getRows );
sqlsrv_close( $conn );

Set the time for a SQL datetime

Thursday, September 16th, 2010

I spent about an hour attempting to google the best way of changing the time of a SQL datetime midquery. I would think this would be easy, and it kind of was, but finding the logic that led me to it was confoundingly difficult. So hopefully you other googlers out there will benefit from my experience. This is for SQL Server, but other versions of SQL will have similar functions available to them.

--We start with DateAdd which adds the specified time interval to a date
DateAdd(timeinterval, amountoftime, date)

--We combine this with DateDiff which gives you the difference in time between two dates. We can ask DateDiff to give us the number of days between the start of time (0) and our current time. Now we have days with no time, essentially zero'ing the time.
DateDiff(timeinterval, starttime, endtime)
DateDiff(dd, 0, mydatetime)

--Finally we combine the two statements. DateDiff gives us our days and DateAdd converts that back into a datetime.
DateAdd(anytimeinterval, 0, DateDiff(dd, 0, mydatetime))

--But what about actually setting the time? No problem, just change "anytimeinterval" and the 0 right after it to the time you want to add
DateAdd(hh, 9, DateDiff(dd, 0, mydatetime)) would add nine hours to the truncated time.