Gossamer Forum
Home : General : Perl Programming :

MySQL problems....

Quote Reply
MySQL problems....
Ok, I have been playing around with MySQL as everyone has been telling me how great it is Cool

I have had a few problems. I manged to connect and disconnect from the database ok, but as soon as I added the INSERT INTO code into it it comes up with errors. Is my formatting of the query buggered up?

The code I am using is;

Code:
#!/usr/bin/perl

eval
{

use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use DBI;

};
Well, thats for any help anyone can give me; I'm really looking forward to using SQL Smile

Andy

if ($@)
{

print "Content-type: text/html \n\n";
print "Unable to find a certain module: $@";

}

# Get the info we need.
$query = new CGI;

$database = "aceinst_test";
$dbh = DBI->connect("DBI:mysql:database=$database", 'aceinst_admin', 'password') || die "Couldn't open database!";

$dbh->insert into members
(username, password, email, fullname, expire) values ('admin', 'password', 'webmaster@ace-installer.com', 'Andrew Newby', '12') || die "Unable to insert info";
$dbh->disconnect;

print "Content-type: text/html \n\n";
print "Done";

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: MySQL problems.... In reply to
You can't use eval with "use" - change use to require or just remove eval and use fatalsToBrowser.

Change || die "Unable to insert info"; to:

Code:
|| &error("Unable to insert info : $DBI::errstr");
Then add:

Code:
sub err {

my ($msg) = shift;
print $query->header;
print $msg;
exit();

}
You need to make sure you use $dbh and $sth correctly too.

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: MySQL problems.... In reply to
Mmm, ok, I'll try it!

And I think you mean;

Code:
sub error {
my ($msg) = shift;
print $query->header;
print $msg;
exit();


}
Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: MySQL problems.... In reply to
Here's a quick eg for you to work with:

Code:
my $user = "DB_USERNAME";
my $pass = "DB_PASSWORD";
Code:
$dbh = DBI->connect( "dbi:mysql:DATABASE_NAME:localhost", $user, $pass ) || &error("Database error : $DBI::errstr");
$sth = $dbh->prepare(qq{
INSERT INTO members (username, password, email, fullname, expire)
VALUES ('admin', 'password', 'webmaster@ace-installer.com', 'Andrew Newby', '12')
}) || &error("$DBI::errstr");

$sth->execute() || &error("$DBI::errstr");
$dbh->disconnect() || &error("$DBI::errstr");
Code:
print $query->header;
print "Done";
Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: MySQL problems.... In reply to
Yeah sorry sub error {

I was copying from one of my scripts and forgot to rename it.

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: MySQL problems.... In reply to
Just something else I noticed......when using if ($@) { you need to print text/plain headers

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: MySQL problems.... In reply to
Just tried the corrections you suggested, but I still get an error;

[Wed Aug 8 14:08:10 2001] new-members.cgi: Execution of /home/ace-inst/public_html/cgi-bin/test/new-members.cgi aborted due to compilation errors.
syntax error at /home/ace-inst/public_html/cgi-bin/test/new-members.cgi line 17, next token ???
[Wed Aug 8 14:08:10 2001] new-members.cgi: Bareword found where operator expected at /home/ace-inst/public_html/cgi-bin/test/new-members.cgi line 16, near "->insert into"

Does this mean that the;

$dbh->insert into members
(username, password, email, fullname, expire) values ('admin', 'password', 'webmaster\@ace-installer.com', 'Andrew Newby', '12') || &error("Unable to insert info : $DBI::errstr");

line has a syntax error in it?

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: MySQL problems.... In reply to
If you use the code I pasted then you won't get an error. But yes....the insert line is the problem

I suggest you take a boo here:

http://www.perldoc.com/cpan/DBI.html

Mods:http://wiredon.net/gt/download.shtml
Installs:http://wiredon.net/gt/


Quote Reply
Re: MySQL problems.... In reply to
Yeah, just found a cool tutorials site for SQL programming at http://www.sqlcourse2.com/ Smile

Andy

webmaster@ace-installer.com
http://www.ace-installer.com