My fellow Perl Monks,

I've come across an issue with using DBI/DBD::mysql when connecting to a remote MySQL database.

I can achieve the connection and query it just fine, however it seems that when I connect to a remote host I am required to specify the tables in my statements in the 'database.table' format instead of being able to shorthand it to just 'table' like all the documentation would lead me to believe was possible (per examples in DBD::mysql).

As an example of my connection code...

my $dsn = "dbi:$driver:database=$database;host=$host;port=$port"; my $dbh = DBI->connect( $dsn, $cfg->{username}, $cfg->{password}, { AutoCommit => 1, RaiseError => 0, PrintError => 0, PrintWarn => 0, }, );

Again, I am able to connect and query the database just fine, but if I do not prepend the database name to the table in my SQL statements, it will return an error.

An example SQL statement...

SELECT * FROM group

The database will return the following...

You have an error in your SQL syntax; check the manual that correspo +nds to your MySQL server version for the right syntax to use near 'gr +oup' at line 2

All I have to change to make it work is prepending "$database." to my table 'group'.

If I check the dbh attribute Name, it will return the entire dsn minus the 'dbi:mysql:' at the front of it.

I have even tried doing a $dbh->do("USE $database;") on the connection, but it still requires me to prepend the database name to my table names in my SQL statements.

Is this expected behavior?


In reply to Solved: DBI and MySQL not setting database properly by KolassaRaptor

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.