Perl + Mysql - Can someone gimme a quick..

They have: 109 posts

Joined: Apr 1999

Can someone show me a quick example of connection to
a mysql database, and choosing the table in the database and than printing an entry into the database (say 4 variables into the 4 different columns the datbase consists of).

They have: 109 posts

Joined: Apr 1999

Thanks! So does this look like it will work?

$database="databasename";
$user="user";
$password="password";
$tablename= "nameoftable";

#Connect to the MySQL server
$dbh = Mysql->connect(undef, $database, $user, $password);
$dbh->selectdb($database);

#Prepare the sql statement
$insert_statement ="$dbh->prepare(qq(insert into $tablename values('$file','$path','$size','$url')))";
#Execute the sql statement
$sth = $dbh->query($insert_statement);
mysql_close()
}

They have: 109 posts

Joined: Apr 1999

Thanks! So does this look like it will work?

$database="databasename";
$user="user";
$password="password";
$tablename= "nameoftable";

#Connect to the MySQL server
$dbh = Mysql->connect(undef, $database, $user, $password);
$dbh->selectdb($database);

#Prepare the sql statement
$insert_statement ="$dbh->prepare(qq(insert into $tablename values('$file','$path','$size','$url')))";
#Execute the sql statement
$sth = $dbh->query($insert_statement);
mysql_close()
}

----------
[Dass]
[Email:[email protected]]-[ICQ:16560402]

They have: 99 posts

Joined: May 1999

Here is some code I use:

$table = "table_name";
$database = "database_name";
$data_source = "dbi:mysql:database_name:localhost:";

# connect to webserver database
$dbh = DBI->connect($data_source)
or &error_handler("database_connect" ) ;

##sql statement to select appropriate records
$statement = qq(select distinct fieldname_1,fieldname_2 from table_name where fieldname_to_match like 'match_value');

my $sth = $dbh->prepare($statement) or &error('database_prepare' );
unless($sth->execute) {
&error_handler("database_prepare" ) ;
}
my($fieldname_1,$fieldname_2);
$sth->bind_columns(undef, \$fieldname_1, \$fieldname_2);
$sth->execute;
while($sth->fetchrow_arrayref) {
}

# a normal select/execute/fetch sequence
$statement = qq{INSERT INTO other_table ( fieldname_1, fieldname_2, other_fieldname3, other_fieldname4 ) VALUES( '$fieldname_1','$fieldname_2','$other_fieldname3','$other_fieldname4')};
$sth = $dbh->prepare($statement);
$sth->execute
or &error(print "Unable to execute $statement", $dbh->errstr);

# close the open statement
$sth->finish;

If you find a good source of example code, please let me know. I have had a H--l of a time trying to get the syntax down.

Hope it helps!

----------
Alan Izat

They have: 99 posts

Joined: May 1999

Dass:
Sorry I did not get back to you until now. I have been swamped. I will be honest here, I did not test your script out, but I would drop the double $dbh= statements and also specify the columns you are dropping the values into:

$database="databasename";
$user="user";
$password="password";
$tablename= "nameoftable";

#Connect to the MySQL server
$dbh = Mysql->connect(undef, $database, $user, $password);

#Prepare the sql statement
$insert_statement ="$dbh->prepare(qq(insert into $tablename (fieldname_1, fieldname_2, fieldname3, fieldname4) values('$file','$path','$size','$url')))";
#Execute the sql statement
$sth = $dbh->execute;
$sth->finish;

You have probably got it sorted out by now. If not, let me know and I will try to test the code. As I said in my first response. I have had a h--l of a time getting the syntax down myself.

Good Luck!

----------
Alan Izat

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.