Re: Here's a DB example
| From: | Martin Cameron | Date: | Fri, 16 Mar 2001 03:20:06 +0000 |
| Subject: | Re: Here's a DB example | ||
| References: | 1 | Groups: | php.gtk |
| Request: | Send a blank email to php-gtk+get-900@lists.php.net to get a copy of this message | ||
As a relative newbie to this 'art' -gtk - I have tried to hack Tarique's
script to do a small job - display the results returned from a MySQL
database query. However I'm having problems getting the array returned into
the $label variable. It's only displaying the last row.
That's the problem in a nutshell.
Here it is in detail.
The "ret_data" function is like this:
function ret_data(){
global $label, $entry, $db, $button1, $button2;
$username=$entry->get_text();
$label->set_text("Connecting to database");
$sql="select * from statement where username = '$username'";
$res=$db->query($sql);
//The query for the default username returns nine rows.
while($row=mysql_fetch_array($res)){
$l= ("Username: ".$row["username"].":: Amount:
".$row["amount"]." :: Date:".$row["date"]." .\n");
//print to the console, the returned value.
print $l;
//This line only returns the last entry. It doesn't concatenate
each entry as it does for the console.
$label->set_text($l);
}
}
I suspect I'll have to create a new "label" for each entry returned which
is not what I want to do - what happens if I've got a large number of
entries to be returned.
If I can get this function to work, then I'll be able to do a lot of work
returning values from a MySQL database.
Regards
Martin Cameron
PS The complete file is as follows:
<?
dl('php_gtk.so');
require('./mysql.inc');
$db=new DB_Sql;
function delete_event(){
return false;
}
function destroy(){
Gtk::main_quit();
}
function show_entry(){
global $entry,$button1,$button2;
$entry->show();
$button3->hide();
$button2->hide();
$button1->show();
}
function ret_data(){
global $label, $entry, $db, $button1, $button2;
$username=$entry->get_text();
$label->set_text("Connecting to database");
$sql="select * from statement where username = '$username'";
$res=$db->query($sql);
$i=0;
while($row=mysql_fetch_array($res)){
$l= ("Username: ".$row["username"].":: Amount:
".$row["amount"]." :: Date: ".$row["date"]."
.\n");
print $l;
$label->set_text($l);
$i++;
}
}
$window= & new GtkWindow();
$window->connect('destroy',destroy);
$window->connect('delete_event',delete_event);
$window->set_border_width(10);
$window->set_title('MySQL Query');
$box1=& new GtkVbox(false,10);
$box1->set_border_width(10);
$window->add($box1);
$box1->show();
$entry= & new GtkEntry();
$entry->set_text('cameron');
$entry->select_region(0,10);
$box1->pack_start($entry);
$entry->show();
$frame=&new GtkFrame('Status');
$label=&new GtkLabel("Status Area.");
$frame->add($label);
$label->show();
$box1->pack_start($frame);
$frame->show();
//Creates button1,connects with appropiate signals and packs
button1 with box1
$button1 = &new GtkButton('Search');
$button1->connect('clicked', "ret_data");
$box1->pack_start($button1,false);
$button3=&new GtkButton("Cancel");
$button3->connect('clicked',"destroy");
$box1->pack_start($button3,true);
//Creates button2,connects with appropiate signals and packs
button2 with box1
$button2 = &new GtkButton('Search Again!');
$button2->connect('clicked', "show_entry");
$box1->pack_start($button2,false);
//Shows toplevel window and hides button2
$window->show_all();
$button2->hide();
/* Run the main loop. */
Gtk::main();
?>
"Tarique Sani " wrote:
> http://www.sanisoft.com/db_ex.zip
>
> Here is a completely useless but profusely commented and illustrative
> example of using a DB with php-gtk, see readme.txt if needed
>
> I hope it will be useful to in-duh-viduals like me who are struggling
> rather successfully to learn GTK ;-)
>
> Tarique
>
> --
> =========================================
> B2B Application Providers
> http://www.sanisoft.com
> Vortal for Nagpur http://nagpurcity.net
> =========================================
>
> --
> PHP GTK Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-gtk-unsubscribe@lists.php.net
> For additional commands, e-mail: php-gtk-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net