chris68805 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm getting an errror: DBD::mysql::st execute failed: MySQL server has gone away, What does it mean?

Replies are listed 'Best First'.
Re: DBI Question
by runrig (Abbot) on Jun 13, 2008 at 19:22 UTC
    You seem to be aware that DBI is a way to do that. So, using DBI, create a database handle and make a single query. Now make another query using the same database handle. And another. You've just made multiple queries. If you need more help, post a little code (and output, error messages, etc.) to show what you've tried and what you're having trouble with.

    Update: Thank you soooo much for updating your post so that my answer (and the next couple) now seem to make no sense at all. If you have a different question, consider posting a different node, replying to your own node, or adding to your original node. Please don't completely change your original node. Your post gets a "--" from me.

Re: DBI Question
by psini (Deacon) on Jun 13, 2008 at 19:27 UTC
    • Open database connection
    • Execute query
    • Repeat step 2
    • Close connection

    You could see the tutorial Class::DBI Intro on the use of DBI to access a database

    Careful with that hash Eugene.

Re: DBI Question
by starX (Chaplain) on Jun 13, 2008 at 19:44 UTC
    To address the issue of multiple queries, you can use an open database handle to prepare a query into a statement handle, and then execute it later. i.e.
    # code that connects to your database here my $first_query = $dbh->prepare('SELECT * FROM myTable'); my $second_query = $dbh->prepare('UPDATE myTable SET myField = ?'); $first_query->execute(); $second_query->bind_param(1, 'some string'); $second_query->execute(); # etc.
    If you want some more specific help than that, please give us some code to look at. If you need more general help about how to use the DBI, you might want to try here or here.
Re: DBI Question
by radiantmatrix (Parson) on Jun 16, 2008 at 17:44 UTC

    MySQL server has gone away means that you're no longer connected to the server. Either you have disconnected (e.g. you've let your database handle go out of scope) or your server is dropping the connection on its end.

    It's a problem you'll have to address by checking to see if the connection is alive and re-connecting as needed. One way to do this is using DBIx::Abstract:

    use DBIx::Abstract; my $dbx = DBIx::Abstract->connect(...); sub do_query { $dbx->ensure_connection; ## do the query; }
    If you're not going to use DBIx::Abstract, the same basic idea can be had from simple DBI:
    $dbh = DBI->connect(...); sub do_query { my $connect; eval { ($connect) = $dbh->selectrow_array('SELECT 1'); }; if ($@ || !$connect) { $dbh = DBI->connect(...) or die "Can't reconnect: $DBI::err"; + } ## do query. }

    I do recommend DBIx::Abstract, though -- you can still get at the database handle from it, but it makes moving from MySQL to PostgresQL or Oracle a snap!

    <radiant.matrix>
    Ramblings and references
    “A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” — Herm Albright
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
      Another option: See the documentation for DBD::mysql and search for 'reconnect'.