frazap has asked for the wisdom of the Perl Monks concerning the following question:
I need to make a html form that can be used to retrieve records from a mysql database. The database is on a server and I have to connect to and retrieve a database handler. For now, I'm able to display the form and check the values send. I still have to talk to the database but I'm stuck with doing the connection. Here is my code
use Mojolicious::Lite; use Mojolicious::Plugin::Database; use Data::Dumper; # Route with placeholder get '/invtot' => sub { my $c = shift; # my $foo = $c->param('foo'); # $c->render(text => "Hello from $foo."); $c->render("invtot"); post '/invtot' => sub { my $c = shift; my $p = $c->req->body_params->to_hash; print Dumper $p; } }; sub startup { my $self = shift; print "startup\n"; $self->plugin('database', { dsn => 'DBI:mysql:host=mysql....', username => 'd......r', password => '.....', options => { 'pg_enable_utf8' => 1, AutoCommit => 1, PrintErr +or =>0, RaiseError => 1 }, helper => 'db', }); } # Start the Mojolicious command system app->start; __DATA__ @@ invtot.html.ep <!DOCTYPE html> <html> <head><title>Paper Journals from the library</title></head> <body> <h1>Paper Journals from the library</h1> <p>Select Title, ISSN, or Domain. Domains are 'Earth Sciences','Ma +thematics','Life Sciences','Chemistry','Physics','Environmental Scien +ces</p> <form name='fSearch' method='post' action=invtot> <table width='50%' border='0'> <tr> <td valign='top'> <select name='ZLField'> <option value='jrn.ti' selected>Title</option> <option value='dom'>Domain</option></select></td> <td valign='top'><select name='ZLMode'> <option value='1' selected>Begin with</option> <option value=2>Anywhere in</option> <option>Exact matching</option> </select> </td> <td valign='top'> <input type='text' name='ztSearch' size='40' maxlength='100'> </td> </tr> </table> <table border='0'> <tr><td valign='top'> Or enter some words from the title in any order. Use \" to search a ph +rase, and * for truncation. Choose AND to have all the words, OR to h +ave any of them. <input type='text' name='ztwords' size='40' maxlength='100'> <input type='radio' name='ccbool' value='AND' checked>AND <input type='radio' name='ccbool' value='OR'>OR <input type='radio' name='ccabo' value='ABO'>ABO </td> </tr><tr> <td> <br><input type='submit' name='Search' value='Send' id='mySubmit'> <input type="reset" name="Submit2" value="Reset" id="myReset"> </td> </tr> </table> </form> </body> </html>
The startup function is taken directly from the documentation of Mojolicious::Plugin::Database but it is not called. And even if it were, how do I retrieve a $dbh I could prepare my sql queries with ?
UpdateAfter reading the doc for different plugins, I have this
use Mojolicious::Lite; use Mojolicious::Plugin::Database; use Data::Dumper; plugin 'Database' => { dsn => 'dbi:MySQL:host=mysql...:dbname=d...', username => 'd...', password => '....', options => { 'pg_enable_utf8' => 1, AutoCommit => 1, PrintErr +or =>0, RaiseError => 1 }, helper => 'db', }; # Route with placeholder get '/invtot' => sub { my $c = shift; # my $foo = $c->param('foo'); # $c->render(text => "Hello from $foo."); $c->render("invtot"); post '/invtot' => sub { my $c = shift; my $p = $c->req->body_params->to_hash; print Dumper $p; print Dumper $c->app->attr('_dbh_db') } }; # Start the Mojolicious command system app->start; __DATA__ #as above
But the page is not even load...
Edit2 With a corrected syntax, the page load.... But I'm still stuck with retrieving the $dbhfrazap
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can I get a database handler with Mojolicious::Plugin::Database
by Veltro (Hermit) on Apr 03, 2019 at 08:17 UTC | |
by frazap (Monk) on Apr 03, 2019 at 09:11 UTC | |
by Veltro (Hermit) on Apr 03, 2019 at 10:31 UTC | |
by frazap (Monk) on Apr 03, 2019 at 11:36 UTC | |
by Corion (Patriarch) on Apr 03, 2019 at 09:27 UTC |