Revisited: Building Cross Platform GUI Apps With PHP-GTK - A PHP-GTK database app
(Page 9 of 10 )
One of the cool things about PHP-GTK is that it integrates seamlessly with PHP, meaning that we can still use all of PHP's regular functions in our PHP-GTK applications. The most popular database to use with PHP is MySQL, so let's create a PHP-GTK app that connects to a MySQL database and retrieves some records.
Enter the following commands at the MySQL console application:
create database people;
use people;
create table programmers
(
pId int auto_increment,
pName varchar(50),
pAge tinyint,
primary key(pId),
unique id(pId)
);Add some people to our programmer's table with the following insert commands:
insert into programmers values(0, 'Bill Smith', 23);
insert into programmers values(0, 'James Black', 45);
insert into programmers values(0, 'Lyonel Jones', 18);
insert into programmers values(0, 'Phil Brown', 22);
insert into programmers values(0, 'Michael Clay', 56);Now let's create a PHP-GTK app that connects to our database and retrieves the records from our programmers table. The app is a bit large to post here in its entirety, so download the
support material for this article if you want to test it on your machine.
Our app starts by creating several labels and text boxes, as well as one command button, which, when clicked will connect to a MySQL server:
$button = &new GtkButton("Connect and Get Rows");
$button->connect("clicked", "getdata");
$box->pack_start($button);
The getdata function connects to MySQL using the host, username and password values from the GtkEntry controls in our app:
function getdata($theButton)
{
global $server;
global $user;
global $pass;
...
$dServer = $server->get_text();
$dUser = $user->get_text();
$dPass = $pass->get_text();
$s = mysql_connect($dServer, $dUser, $dPass) or die("Couldn't connect to database server");
$d = mysql_select_db("people", $s);
Next, all of the controls that occupy our GtkVBox widget are hidden:
// Hide all controls
$label1->hide();
$label2->hide();
$label3->hide();
$server->hide();
$user->hide();
$pass->hide();
$button->hide();It then queries the programmer's table, looping through each result and adding it to a string variable:
$result = mysql_query("select * from programmers order by pName asc");
$pList = "";
while($row = mysql_fetch_array($result))
{
// Create a string array
$pList .= $row["pName"] . " is " . $row["pAge"] . "\n";
}It then creates a new GtkLabel widget and assigns the value of the $pList variable to the label by passing it to the labels constructor. The label is then added to the GtkVBox widget, which is part of our main window:
// Create a GtkCList and show it
$p = &new GtkLabel("$pList");
$box->add($p);
$p->show();Here's how the window looks before and after I enter my database credentials and click on the connect button:


As you can see, the GtkLabel and GtkEntry objects are hidden, and the new label is displayed inside of out GtkVBox object.
Next: Conclusion >>
More PHP Articles
More By Mitchell Harper