Using MySQL with GTK--------------------------------------------------------------------------------
Created By: Adam Rambousek
Created On: 2001-03-29Surely, it's not exaggerative to claim, that PHP-MySQL is one of the most used combinations on servers. So why not to use the strong combo Gtk-PHP-MySQL?I think that the major difference between serverside PHP and clientside Gtk-PHP, is that in first case PHP script is connecting to the MySQL dbase located on the same server (localhost), but in the second case client applications will use database on other then local computer.So, at first we need to set up the MySQL server. Login to mysql as root and add user to the table mysql.user. This user will access MySQL from remote computers, so the host should be set to '%' (or the names/IPs of the computers). Let's say, we add user gtkuser with password gtkpassword. And our MySQL server is running on computer called dbserver.Then we can write the Gtk-PHP script. I've written simple application, that takes SQL query from user, sends it to the MySQL server and then displays the result in new window. You can download the script, from here. I'll mention interesting parts concerning MySQL.Just before starting Gtk::main(); loop, let the app connect to MySQL server. Notice, you must change dbserver,gtkuser and gtkpassword to theirs true values.
mysql_pconnect("dbserver","gtkuser","gtkpassword") or die("can't connect to server");Another interesting part is displaying the result of query. You can see, it uses standard PHP functions for MySQL, so it's nothing special :-)
<?    /*here we deal with the result
      if mysql_query called with the query entered in main window, return a result
      we can display clist
    */ 
    if ($result = mysql_query($query->get_text())) {
        /*
          at first, keys array contains the names of columns
        */
        if ($data = mysql_fetch_array($result)) {
            $i=0;
            $keys = array();
            while (list($key,$val) = each($data)) {
                if ($i%2) $keys[]=$key;
                $i++;
            }
        }
        
        /*
          now we can prepare the clist, keys are the titles of columns and the number
          of columns is equal to the number of keys
        */  
        $clist = &new GtkCList(count($keys), $keys);
        $clist->connect('click_column', 'clist_click_column');        //we sets the auto_resize for each column
        for ($i=0;$i<count($keys);$i++) $clist->set_column_auto_resize($i, true);
        $scrolled_win->add($clist);        /*
          now the data from result
          we get the data from each row to row_data array and then we append this
          array to the clist as a new row
        */
        do {
            for($i=0; $i < count($data)/2; $i++){
                                        $row_data[$i] = $data[$i];                }
                $clist->append($row_data);
        } while ($data = mysql_fetch_array($result));
    }?>Best thing you could do, is to download the script and try it for yourself. I'll be glad for any comments (you can find it here).
Adam Rambousek ([email protected])