Forum Moderators: coopster & phranque

Message Too Old, No Replies

Accessing Mysql database

         

scoobie

9:51 pm on Dec 11, 2005 (gmt 0)

10+ Year Member



Hi,

i am very new to perl and was wndering if anyone can help me. i hav a html form where a user enters their name and clicks submit, this then should call a perl script that connects to a database. i don't know how to do this script. does anybody know any tutorials/code that will help me with a simple script that connects to mysql db and runs one select query

thanks in advanced,

s

Moby_Dim

2:59 pm on Dec 12, 2005 (gmt 0)

10+ Year Member



Do something like this :

use DBI;
my(%form) = &parseform(); #subrutine to parse your form
my($field1) = $form{'field_one'};
my($field2) = $form{'field_two'};

my($db_user) = user_logi;
my($db_pass) = password;
my($db_name) = the_name_of_mysql_db;
my($db_host) = host_name;
my($db_port) = '3306'; #the usual value

my($dbh) = "DBI:mysql:$db_name:$db_host:$db_port";
$dbh = DBI->connect($dbh, $db_user, $db_pass);

my($qur) = "SELECT...WHERE...etc"; #sql query where you can use the field1 and field2 values of form hash;

my($sth)=$dbh->prepare($qur);
$sth->execute;

#next is to use one of the appropriate methods e.g. fetchrow_arrayref() e.g:
while($n=$sth->fetchrow_arrayref()) {
#... do something with $n
}
$dbh->disconnect();
...

simon2263

9:20 am on Dec 13, 2005 (gmt 0)

10+ Year Member



Try this link:

[washington.edu...]

or do a google search for "perl mysql tutorial"

scoobie

11:08 am on Dec 13, 2005 (gmt 0)

10+ Year Member



thanks, i got it to work