|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I want to query a mysql table using perl.
I want to issue this command upon connecting: 'select * from MODEMS;' and read the output to an array/hash variable. So far, I have the following code: ----------------------------------------------------------- use DBI; use Socket; my $database_name = "rota1"; my $location = "localhost"; my $port_num = "3306"; my $database = "DBI:mysql:$database_name:$location:$port_num"; my $db_user = "luser"; my $db_password = "lusers-password"; my $dbh = DBI->connect($database,$db_user,$db_password); $sth->finish; $dbh->disconnect; ------------------------------------------------------------ What do you think I am missing? Where shall I put the mysql statement? thankyou all. |
|
#2
|
||||
|
||||
|
ok..
i havent used mysql with perl but still, i would say that onceu have made connection using my $dbh = DBI->connect($database,$db_user,$db_password); u would have one variable that is $dbh associated with ur connection. now use this $dbh to query ur database. so there can be some method like query. i am not sure. check doc about DBI. jd
__________________
_____________________________ d.k.jariwala (JD) ~ simple thought, simple act ~ I blog @ http://jdk.phpkid.org |
|
#3
|
||||
|
||||
|
You're so close!
You use the $dbh connection scalar to prepare, execute and use other DBI methods. (BTW, I haven't checked the syntax of your connection. This is all simple documentation stuff, and there are tutorials for working with DBI all over the place. Talk to my best friend, www.google.com) So, to prepare and execute a query once your connection is setup properly- Code:
# Create the query
my $query=$dbh->prepare('select * from table1 group by field1 order by field2')
or die('Couldn\'t prepare query:',$dbh->errstr());
#Do the query
$query->execute() or die('Couldn\'t execute query:',$dbh->errstr());
#Do something with what's retrieved from the query
while(my($field1,$field2,$field3)=$query->fetchrow_array()){
#Stuff is done with the fields retrieved here.
}
$query->finish();
The most important thing, in my opinion, is to status check every call to DBI through $dbh or other DBI objects. If something fails, it will tell you exactly what happened and make you debugging MUCH easier. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Query MySQL with Perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|