Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: DBI::mysql fetch() without execute() PROBLEM

by cfreak (Chaplain)
on Jan 22, 2002 at 01:00 UTC ( [id://140490]=note: print w/replies, xml ) Need Help??


in reply to DBI::mysql fetch() without execute() PROBLEM

You get that error because there is a problem with your execute statement. It doesn't complete and then fetch without a proper execute causes your program to die. Changing

my $rc = $sth->execute('OH');

To:

$sth->execute('OH') or die $sth->errstr;

Should give you the error. I would venture to guess its in your original statement. BTW I don't believe capturing the output from the execute statement is particularly useful. I've never seen that used

Hope that helps

Chris

Replies are listed 'Best First'.
Re: Re: DBI::mysql fetch() without execute() PROBLEM
by peppiv (Curate) on Jan 22, 2002 at 01:18 UTC
    Yes! You were right. It was in my original statement. In fact, it says no database selected. (Which is obvious now why it didn't return any results). Even though I included the exact and full path.

    Is there a better way to select the database if it's in a different directory other than f_dir= ?

      I should have seen that right away. You are trying to connect to a mysql database with syntax for the DBI::CSV module. When using the mysql driver connect like this:

      my $dsn = "DBI:mysql:database=your_db_name;host=your_hostname;"; my $dbh = DBI->connect($dsn,'your_db_username','your_db_password') or +die "Couldn't connect to database";

      So instead of "f_dir" use "database=" and make sure to put the host name. If you are trying to use a comma separated file somewhere on your system then you should use the CSV driver, just keep using the f_dir part and put "CSV" where you have "mysql".

      Hope that helps

      Chris

        Thanks for the catch. I actually had it that way once, but since I couldn't get it to work I dropped it.

        #!/usr/bin/perl -w use strict; use DBI; use CGI::Carp qw(fatalsToBrowser); use CGI qw (:standard); print "Content-type: text/html\n\n"; my $dbh = DBI->connect(qq{DBI:mysql:database=subscriptions;host=l +ocalhost,I DON'T HAVE A USERNAME AND PASSWORD OTHER THAN ROOT}, {Rais +eError => 1} ); my $sth = $dbh->prepare( q{SELECT email FROM contacts WHERE state + = ?} ); $sth->execute('OH') or die $sth->errstr; while (my @result = $sth->fetchrow_array()) { print "@result\n"; } # check for problems which may have terminated the fetch early die $sth->errstr if $sth->err; $dbh->disconnect;

        I've tried many things to connect but to no avail. \s tells me that I'm connected via a UNIX socket. I can only find docs about different ports. Could this be a problem?

        Can't call method "prepare" on an undefined value at /usr/local/etc/ht +tpd/cgi-bin/DBI_mysql_01.pl line 12.

        Is there any way to view the path or connection?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://140490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found