PHP Desktop Applications

PHP Desktop Applications

Although PHP is primarily used for web development, it can be used to create regular desktop applications. Now that you know it is possible(and has been for quite some time), you may want to be informed about your possible options.

A few options you have are PhpDesktop, PHP-GTK, PHP Nightrain and Exeoutput. All of the previously mentioned options are free except for Exeoutput.

The remainder of this tutorial will cover the PhpDesktop and long time package PHP-GTK.


PHPDesktop

PHP is a very simple application that can be used to create a desktop application in minutes. After you download and unzip the zip file, you move the folder anywhere you want. Then, you click on the 'exe' file to run the application. The application itself is basically Chrome browser that can interpret typical PHP, SQL, html and css. Unlike a typical web application, it can run independently using an SQLite database file(which is included in the example).

All in all, it is essentially a web application that can run all by itself in its own folder. However, you could use a remote database if you desired. But, if you intend to have others download and use your custom desktop application you may want the portable Sqlite database since it is nice, convenient, movable file and it runs very fast.

PHPDesktop can be downloaded from Google at the url https://code.google.com/p/phpdesktop/

MYSQL vs Sqlite

Since using Sqlite is such a quality choice for your database, you will be able to use your mySQL skills and manipulate the database with a free tool like Sqlite Studio which be downloaded from their website or Sqliteman. This tool makes it very easy to run queries, create tables and alter databases to your liking.

Alternatively, you could use Linux and run the Sqlite console much like a mySQL console.


Using Sqlite with Linux Command Line

The example below explains how to access the sqlite> prompt. Its usage can be found at sites like http://www.tutorialspoint.com/sqlite/sqlite_commands.htm and http://www.sqlite.org/cli.html.

root# sqlite3 my_database.sqlite

Although using Sqlite is very similar to mySQL, you should know that many mySQL queries like 'mysqli_query' and 'mysqli_fetch_assoc' will not work with Sqlite. You will need to connect to the database of your php application using PDO(PHP Data Objects). The example below will show how to connect with PDO and output the rows from a table.

In addition to querying the database, you could face other slight differences with database usage. For example, with mySQL, you could have a field with the 'int' type that is autoincrement. With Sqlite, you would change that type to 'INTEGER' and make it a primary key.

Using PDO for database connections in your projects can make it easier to port a mysql application to a Sqlite application.

function PDO_Connect($mydb_file)
{
global $PDO;
$mydb_file;
$user="";
$password = "";
$PDO = new PDO($mydb_file, $user, $password);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}

$db_file = "./sqlite-database.db";
PDO_Connect("sqlite:$db_file");

$command = "SELECT DISTINCT id, url FROM urls WHERE enabled='1' ";
$sth = $PDO->prepare($command);
$sth->execute();
$result = $sth->fetchAll();

foreach($result as $key => $val) {
$urls[] = $val['url'];
}
}

The code below above uses the PDO_Connect() function to connect to the database. After that, the statement is prepared, executed and an array is returned with the name '$result'. Finally, each element of the array is parsed and another array of each values is added to the '$urls' array.

Converting a mySQL Database to Sqlite Database

If you plan to create a Sqlite database from mySQL database, you can do it easily with the linux command line. An example is shown below for making the conversion. The script mysql2sqlite.sh can be downloaded here from Github.

root# apt-get install sqlite3

root# ./mysql2sqlite.sh -u username -pMyPassword database_from | sqlite3 database_to.sqlite


PHP-GTK

PHP-GTK has been around since its first release in 2001. Although the web site and code look a little dated, it still works. PHP-GTK can be downloaded from http://pigii.sourceforge.net/

PHP-GTK is very simple to use. You download the file and install it. Often, it install into your Program Files folder. Then, you can move the folder to any location you want; such as the C: folder since this is a very easy location to work with.

Once you have your working folder, you can make a '.cmd' file within it. In the '.cmd' file, you can add a line siimilar to the one shown below.

"%CD%\..\..\Desktop\PHP-GTK.201\php.exe"  "%CD%\demos\components\stock-browser2.php"

The line will run the php.exe file and will open the file located in the 'demos\components' folder. That is the basics to getting started and being able to use PHP-GTK. To build a nice, full blown application is another story.