<?
/*
A little sample script that takes SQL query from user, sends it to the MySQL server and
displays the result of query in new window.
DON'T FORGET TO CHANGE SERVER, USERNAME AND PASSWORD IN MYSQL_PCONNECT FUNCTION!!
I'll be glad for any comments.
Adam Rambousek - rambousek@volny.cz
*/
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
$windows = array();
function delete_event($window, $event)
{
$window->hide();
return true;
}
function close_window($widget)
{
$window = $widget->get_toplevel();
$window->hide();
}
/*
Called when clist column is clicked. It sets sorting by the clicked column.
*/
function clist_click_column($clist, $column) {
$clist->set_sort_column($column);
$clist->sort();
}
/*
Function displaying the result of query.
*/
function do_query($query)
{
global $windows;
//if the query_window is opened, let's close it
if (isset($windows['query_window'])) {
close_window($windows['query_window']);
}
$window = &new GtkWindow;
$windows['query_window'] = $window;
$window->set_name('query_window');
$window->connect('delete-event', 'delete_event');
$window->set_policy(false, true,false);
$window->set_title('Query result');
$window->set_uposition(220,85);
$box1 = &new GtkVBox();
$window->add($box1);
//frame displaying entered sql query
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$label = &new GtkLabel($query->get_text());
$frame->add($label);
//frame displaying clist with the query result
$frame = &new GtkFrame('MySQL Query Result');
$box1->pack_start($frame,true);
//we'll display the result with scrollbars
$scrolled_win = &new GtkScrolledWindow();
$scrolled_win->set_border_width(5);
$scrolled_win->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$frame->add($scrolled_win);
/*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));
}
$button = &new GtkButton('close window');
$button->connect('clicked', 'close_window');
$box1->pack_start($button,false);
$button->set_flags(GTK_CAN_DEFAULT);
$button->grab_default();
$window->show_all();
}
function main_window()
{
$window = &new GtkWindow();
$window->set_policy(false,true,false);
$window->set_name('main_window');
$window->set_title('MySQL Query sample');
$window->set_uposition(80,80);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window->connect_object('delete-event', array('gtk', 'false'));
$box1 = &new GtkVBox();
$window->add($box1);
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$entry = &new GtkEntry();
$frame->add($entry);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Do Query');
$button->connect_object('clicked', 'do_query',$entry);
$box1->add($button);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Quit');
$button->connect_object('clicked', array('gtk', 'main_quit'));
$box1->add($button);
$window->show_all();
}
mysql_pconnect("SERVER","USERNAME","PASSWORD") or die("can't connect to server");
main_window();
Gtk::main();
?>
/*
A little sample script that takes SQL query from user, sends it to the MySQL server and
displays the result of query in new window.
DON'T FORGET TO CHANGE SERVER, USERNAME AND PASSWORD IN MYSQL_PCONNECT FUNCTION!!
I'll be glad for any comments.
Adam Rambousek - rambousek@volny.cz
*/
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
$windows = array();
function delete_event($window, $event)
{
$window->hide();
return true;
}
function close_window($widget)
{
$window = $widget->get_toplevel();
$window->hide();
}
/*
Called when clist column is clicked. It sets sorting by the clicked column.
*/
function clist_click_column($clist, $column) {
$clist->set_sort_column($column);
$clist->sort();
}
/*
Function displaying the result of query.
*/
function do_query($query)
{
global $windows;
//if the query_window is opened, let's close it
if (isset($windows['query_window'])) {
close_window($windows['query_window']);
}
$window = &new GtkWindow;
$windows['query_window'] = $window;
$window->set_name('query_window');
$window->connect('delete-event', 'delete_event');
$window->set_policy(false, true,false);
$window->set_title('Query result');
$window->set_uposition(220,85);
$box1 = &new GtkVBox();
$window->add($box1);
//frame displaying entered sql query
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$label = &new GtkLabel($query->get_text());
$frame->add($label);
//frame displaying clist with the query result
$frame = &new GtkFrame('MySQL Query Result');
$box1->pack_start($frame,true);
//we'll display the result with scrollbars
$scrolled_win = &new GtkScrolledWindow();
$scrolled_win->set_border_width(5);
$scrolled_win->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$frame->add($scrolled_win);
/*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));
}
$button = &new GtkButton('close window');
$button->connect('clicked', 'close_window');
$box1->pack_start($button,false);
$button->set_flags(GTK_CAN_DEFAULT);
$button->grab_default();
$window->show_all();
}
function main_window()
{
$window = &new GtkWindow();
$window->set_policy(false,true,false);
$window->set_name('main_window');
$window->set_title('MySQL Query sample');
$window->set_uposition(80,80);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window->connect_object('delete-event', array('gtk', 'false'));
$box1 = &new GtkVBox();
$window->add($box1);
$frame = &new GtkFrame('MySQL Query');
$box1->pack_start($frame,false);
$entry = &new GtkEntry();
$frame->add($entry);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Do Query');
$button->connect_object('clicked', 'do_query',$entry);
$box1->add($button);
$separator = &new GtkHSeparator();
$box1->pack_start($separator,false);
$button = &new GtkButton('Quit');
$button->connect_object('clicked', array('gtk', 'main_quit'));
$box1->add($button);
$window->show_all();
}
mysql_pconnect("SERVER","USERNAME","PASSWORD") or die("can't connect to server");
main_window();
Gtk::main();
?>
<?
if (!class_exists('gtk')) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
}
function destroy() {
Gtk::main_quit();
}
$window = &new GtkWindow();
$window->set_title("MySQL Manager");
$window->set_usize(300,300);
$window->connect('destroy', 'destroy');
$window->set_border_width(10);
$table=&new gtktable(5,3);
$window->add($table);
//add text boxes, button, and label
$usertext=&new gtklabel();
$usertext->set_text("Username:");
$table->attach($usertext,1,2,1,2);
$user=&new gtkentry();
$table->attach($user,2,3,1,2);
$passtext=&new gtklabel();
$passtext->set_text("Password:");
$table->attach($passtext,1,2,2,3);
$pass=&new gtkentry();
$table->attach($pass,2,3,2,3);
$sertext=&new gtklabel();
$sertext->set_text("Server:");
$table->attach($sertext,1,2,3,4);
$server=&new gtkentry();
$table->attach($server,2,3,3,4);
$conb=&new gtkbutton("Connect!");
$conb->connect('clicked','mconnect');
$table->attach($conb,1,3,4,5);
function put($what,$where) {
$where->set_text($what);
}
function mconnect() {
global $window,$pass,$user,$server;
$pass=$pass->get_text();
$user=$user->get_text();
$server=$server->get_text();
$conn=mysql_connect($server,$user,$pass);
mysql_select_db("simtown");
$q="select * from users";
while ($row=mysql_query($q)) {
print $row[0];
}
mysql_close($conn);
}
$window->show_all();
Gtk::main();
?>
if (!class_exists('gtk')) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
}
function destroy() {
Gtk::main_quit();
}
$window = &new GtkWindow();
$window->set_title("MySQL Manager");
$window->set_usize(300,300);
$window->connect('destroy', 'destroy');
$window->set_border_width(10);
$table=&new gtktable(5,3);
$window->add($table);
//add text boxes, button, and label
$usertext=&new gtklabel();
$usertext->set_text("Username:");
$table->attach($usertext,1,2,1,2);
$user=&new gtkentry();
$table->attach($user,2,3,1,2);
$passtext=&new gtklabel();
$passtext->set_text("Password:");
$table->attach($passtext,1,2,2,3);
$pass=&new gtkentry();
$table->attach($pass,2,3,2,3);
$sertext=&new gtklabel();
$sertext->set_text("Server:");
$table->attach($sertext,1,2,3,4);
$server=&new gtkentry();
$table->attach($server,2,3,3,4);
$conb=&new gtkbutton("Connect!");
$conb->connect('clicked','mconnect');
$table->attach($conb,1,3,4,5);
function put($what,$where) {
$where->set_text($what);
}
function mconnect() {
global $window,$pass,$user,$server;
$pass=$pass->get_text();
$user=$user->get_text();
$server=$server->get_text();
$conn=mysql_connect($server,$user,$pass);
mysql_select_db("simtown");
$q="select * from users";
while ($row=mysql_query($q)) {
print $row[0];
}
mysql_close($conn);
}
$window->show_all();
Gtk::main();
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1093/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-4-27 11:19
评论列表