CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
MySQL problem

 



menchen
User

Apr 24, 2001, 3:19 PM

Post #1 of 4 (234 views)
MySQL problem Can't Post

can someone please help me with the following code?

i am trying to work with MySQL, but the following code doesn't do anything with the database nor return any error.

use CGI qw(:all);
use CGI qw(:cgi-lib);
use DBI;

my $sql_query = $input{'sql_query'};
my $dbh = DBI->connect("DBI:mysql:$db:$server", $login, $password);
my $table_data = $dbh->prepare("$sql_query");

$table_data->execute;

print "Content-type: text/html\n\n";
if (not $dbh) { print "$DBI::errstr"; }
else { print "done"; }

thanks for the help

-jason



BigRich
Novice

Apr 25, 2001, 12:40 AM

Post #2 of 4 (224 views)
Re: MySQL problem [In reply to] Can't Post

Hopefully this isn't the entire script.

use CGI qw(:all :cgi-lib); # no need to "use" CGI twice
use DBI;

my $sql_query = $input{'sql_query'}; # what query are you testing with? This could be the problem.

# You don't check for errors where they could happen

my $dbh = DBI->connect("DBI:mysql:$db:$server", $login, $password) || die $DBI::errstr;

my $table_data = $dbh->prepare("$sql_query") || die $dbh->errstr;

$table_data->execute || die $dbh->errstr;

print "Content-type: text/html\n\n";
if (not $dbh) { print "$DBI::errstr"; }
else { print "done"; }

You need to go over the DBI docs (regarding execute) some more to see why the above doesn't give an error.


It could be a combination of bad code and a bad query, but I can only guess.


BigRich



menchen
User

Apr 25, 2001, 5:58 PM

Post #3 of 4 (217 views)
Re: MySQL problem [In reply to] Can't Post

no, this is not the entire script

and i found the the error at $table_data->execute || die $dbh->errstr with error checking, and the following is $sql_query

the error message is "Incorrect table definition; There can only be one auto column and it must be defined as a key", but i don't know how to fix it

CREATE TABLE phone_book (
id INT NOT NULL AUTO_INCREMENT,
first_name CHAR(20) NOT NULL,
last_name CHAR(15),
birthday CHAR(18),
email CHAR(30),
home_phone CHAR(15),
work_phone CHAR(15),
cell_phone CHAR(15),
pager CHAR(15),
fax CHAR(15),
address CHAR(25),
city CHAR(20),
state CHAR(20),
country CHAR(20),
zip_code CHAR(5),
other CHAR(100)
)

can you please took a look at this and tell me what is wrong with the above?

thanks so much..

-jason



BigRich
Novice

Apr 26, 2001, 12:39 AM

Post #4 of 4 (211 views)
Re: MySQL problem [In reply to] Can't Post

Log into your database and try the following command.

mysql> ALTER TABLE phone_book ADD PRIMARY KEY (id);

I believe the error is from the id field being an AUTO INCREMENT field but it's not a key.

I've never seen that error, but I always use an auto increment field as the primary key.

Try that and see if it helps, check out the mySQL docs for more info on ALTER.

Good luck,

BigRich


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives