TaskFreak! v0.6.2 – Customizing Status

Background Knowledge


The progress of a task in TaskFreak! is shown as a percentage value and is not exactly visually appealing to quickly spot the progress. With a few minor alterations we can show the percentage completed bar that fills as the task progresses and a gradient bar indicating the progress along with the percentage value.

This solution was posted by Searcher at Re: Taskfreak Customizing Status.

Continue reading

How To – Convert MSSQL Timestamp/Datetime to Unix Timestamp

Background Knowledge


I will explain how to convert a DATETIME (data type) value in Microsoft SQL Server to Unix timestamp and how to convert Unix timestamp to DATETIME. A Unix timestamp is a integer value of seconds since January 1, 1970 at midnight. For further explanation of Unix timestamps refer to Wikiepedia, UnixTimestamp.com or http://unixtimesta.mp/.

Note: This solution only work on dates prior to 2038-01-19 at 3:14:08 AM, where the delta in seconds exceeds the limit of the INT data type (integer is used as the result of DATEDIFF). See source for further details as I have not verified a solution to this problem.

Solutions


Convert Datetime Value to Unix Timestamp (today)

1
SELECT DATEDIFF(s, '19700101', GETDATE());

Result: 1305630800

Convert Datetime Value to Unix Timestamp from two Values

1
SELECT DATEDIFF(s, StartTime, EndTime) AS Duration FROM Programs

Result: 3600

Convert Unix Timestamp Value to Datetime Value

1
SELECT DATEADD(s, 123456789, '19700101');

Result: 1973-11-29 21:33:09.000

Source: How do I convert a SQL Server DATETIME value to a Unix timestamp?
Source: DATEADD (Transact-SQL)
Source: DATEADD
Source: DATEDIFF (Transact-SQL)
Source: DATEDIFF

Example of a Basic ODBC (MSSQL Server) Query using PHP

An example of a basic ODBC (MSSQL Server/DSN-Less) query using PHP.

Example of Result Set Returning One Row

1
2
3
4
5
6
7
8
9
$szDBConn="DRIVER={SQL Server};SERVER=SQLServerNameHere;DATABASE=DatabaseNameHere";
$szDBUsername="UsernameHere";
$szDBPswd="PasswordHere";
$szDBQuery="SELECT FooBar, Foo_Bar, Foo_ID FROM FoobarSubscribers WHERE FooID=777";
$rDBConnect = odbc_connect($szDBConn, $szDBUsername, $szDBPswd);  
$rDBRes = odbc_exec($rDBConnect, $szDBQuery);
$szLastCheck = odbc_result($rDBRes, "LastCheck");
odbc_free_result($rDBRes);
odbc_close($rDBConnect);

Example of Result Set Returning Multiple Rows in an Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$szDBConn="DRIVER={SQL Server};SERVER=SQLServerNameHere;DATABASE=DatabaseNameHere";
$szDBUsername="UsernameHere";
$szDBPswd="PasswordHere";
$szDBQuery="SELECT FooBar, Foo_Bar, Foo_ID FROM FoobarSubscribers";
$rDBConnect = odbc_connect($szDBConn, $szDBUsername, $szDBPswd);  
$rDBRes = odbc_exec($rDBConnect, $szDBQuery);
while($obRows = odbc_fetch_object($rDBRes))
{
	print $obRows->ColumnName1."<br />";
	print $obRows->ColumnName2."<br />";
	print $obRows->ColumnName3."<br />";
}
odbc_free_result($rDBRes);
odbc_close($rDBConnect);

Example of Result Set Returning Multiple Rows in an Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$szDBConn="DRIVER={SQL Server};SERVER=SQLServerNameHere;DATABASE=DatabaseNameHere";
$szDBUsername="UsernameHere";
$szDBPswd="PasswordHere";
$szDBQuery="SELECT FooBar, Foo_Bar, Foo_ID FROM FoobarSubscribers";
$rDBConnect = odbc_connect($szDBConn, $szDBUsername, $szDBPswd);  
$rDBRes = odbc_exec($rDBConnect, $szDBQuery);
while($saRows = odbc_fetch_array($rDBRes))
{
        print $saRows['ColumnName1'];
        print $saRows['ColumnName2'];
        print $saRows['ColumnName3'];
}
odbc_free_result($rDBRes);
odbc_close($rDBConnect);

Comments/description of Example

Line #1
Database connection string to define the driver, server host name and database name.
The “server host name” is the server name or IP address of your database server. If your host has multiple instances the “host” value would be formatted like so “foo\bar”. If your using SQL Server Express the “host” name locally would be “.\SQLEXPRESS”.
Line #2-#3
Database login credentilas; username and password.
Line #4
SQL query.
Line #5
Connect to the database defined by Line #1, Line #2 and Line #3.
Line #6
Prepare and execute an SQL statement.
Line #7
Fetch results returned back from the SQL statement. I use a while loop to enumerate through each row of results returned. If you know only one row is going to be returned the while loop is not necessary.
odbc_free_result()
Close database connection.
odbc_close()
Close database connection.

PHP – mssql_query() Error

Background Knowledge


I’m running PHP v5.2.9-2 with SQL Server 2005 Express on Windows Server 2003 R2 SP2.

Error Message


Warning: mssql_query() [function.mssql-query]: message: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. (severity 16)

Solution


Specify the database columns within your database query (select filed1, field2 from foo). Avoid doing queries with the wildcard (*), select * from foo.

Source: PHP Manual on MSSQL_Query()

Example of a Basic MSSQL Query using PHP

An example of a basic MSSQL (Microsoft SQL Server/SQL Server Express) query using PHP.

1
2
3
4
5
6
7
8
9
$szQry = "SELECT column1, column2 FROM foo";
$szDBConn = mssql_connect("host","username","password");
mssql_select_db("database_name", $szDBConn);
$saResults = mssql_query($szQry, $szDBConn);
while($obResults = mssql_fetch_row($saResults))
{
   echo $obResults[0]." ".$obResults[1];
}
mssql_close($szDBConn);

Comments/description of Example

Line #1
SQL statement that will be sent to the MySQL database server.
Line #2
MSSQL database login credentilas; host (127.0.0.1), username and password.
The “host” is the server name or IP address of your database server. If your host has multiple instances the “host” value would be formatted like so “foo\bar”. If your using SQL Server Express the “host” name locally would be “.\SQLEXPRESS”.
Line #3
Select database using the login credentials provided above.
Line #4
Send SQL statement to database.
Line #5-8
Fetch results returned back from the SQL statement. I use a while loop to enumerate through each row of results returned. If you know only one row is going to be retunred the while loop is not necessary.
Line #9
Close database connection.