Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

Problems with MySQL - "No Database Selected"

by ninja-joe (Monk)
on May 09, 2002 at 20:37 UTC ( #165498=perlquestion: print w/ replies, xml ) Need Help??
ninja-joe has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to do a basic select but I'm running into an error message:
No Database Selected
Here's the code I used (password, domain and user have been changed for privacy purposes)
#!/usr/bin/perl use DBI; use CGI qw/:standard/; import_names('f'); print header(); my $database = "databasename=default"; my $host = "host=mysql.domain.com"; my $driver = "DBI:mysql"; my $user = "user"; my $pass = "pass"; my $dbh = DBI->connect("DBI:mysql:$database;$host", $user, $pass) or print "Can't connect to mysql database: $DBI::errstr<br>"; $sth = $dbh->prepare("SELECT section, content FROM webdata WHERE secti +on=home") or print "Can't prepare statement: $DBI::errstr<br>"; $sth->execute() or print "Can't execute statement: $DBI::errstr<br>"; $dbh->disconnect();
The code doesn't flag any syntax errors, so I'm a little confused. Is it me or something I should email the sysadmin about?

Comment on Problems with MySQL - "No Database Selected"
Download Code
Re: Problems with MySQL - "No Database Selected"
by boo_radley (Parson) on May 09, 2002 at 20:47 UTC
    Add a "CONNECT whatever_your_database_is_named" and you should be ok.
    Sounds like your dba didn't choose a default database, if that's possible in mysql...

    not perl related, and minimally only database related... pick up a primer and enjoy.
    update oops, chromatic's correct. Teach me to write a node while I'm in a cranky mood. mea culpa.
      I did this and it flagged an SQL syntax error: Did I add it to the right spot?
      $sth = $dbh->prepare("CONNECT default; SELECT section, content FROM we +bdata WHERE section=home")
Re: Problems with MySQL - "No Database Selected"
by chromatic (Archbishop) on May 09, 2002 at 21:09 UTC
    I believe $database should be "database=default".

    (As this deals directly with the syntax the MySQL DBD expects, I'd consider it highly Perl related.)

      When I change that I get this:
      DBI::db=HASH(0x823ad6c)->disconnect invalidates 1 active statement han +dle (either destroy statement handles or call finish on them before d +isconnecting) at index.cgi line 24.
Re: Problems with MySQL - "No Database Selected"
by mephit (Scribe) on May 09, 2002 at 21:31 UTC
    my $database = "databasename=default"; should be

    my $database = "database=default";
Re: Problems with MySQL - "No Database Selected"
by ninja-joe (Monk) on May 09, 2002 at 21:42 UTC
    OH SWEET LORDY JAYZUS! HOLD THE PRESSES! I ANSWERED MY OWN QUESTION!! To those whom it may concern, I needed to include $sth->finish(); right before my disconnect. Thanks all you guys for being patient with my dumb ass.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://165498]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-04-20 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls