I'm starting with Mojolicious.

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 ?

Update

After 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 $dbh

frazap


In reply to How can I get a database handler with Mojolicious::Plugin::Database by frazap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.