I want to print all sql result of my perl script, I've connected to the database and I want to show the result of my sql query:
MySQL("SELECT * FROM test");
# define subroutine to submit MySQL command
sub MySQL
{
#Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=database;host=ip",
"login", 'password',
{'RaiseError' => 1});
my $query = $_[0]; #assign argument to string
my $sth = $prepare($query); #prepare query
$sth->execute(); #execute query
while (my @row = $sth->fetchrow_array)
{
print "@row\n";
}
}
I have this errors:
Global symbol "$prepare" requires explicit package name at test3.pl line 34.
syntax error at test3.pl line 34, near "$prepare("
Global symbol "$sth" requires explicit package name at test3.pl line 36.
Execution of test3.pl aborted due to compilation errors.
Change this line:
my $sth = $prepare($query);
to
my $sth = $dbh->prepare($query);
and don't forget to close the $sth
and $dbh
handles after the while loop with:
$sth->finish;
$dbh->disconnect;
thanks @Miguel Prz,
i want to use this query but not work:
MySQL("SELECT CONCAT(HOST, ',', e.name), e.severity, location_lat, location_lon
FROM hosts
LEFT JOIN hosts_groups ON (hosts.hostid = hosts_groups.hostid AND hosts.host NOT LIKE '%Template%')
LEFT JOIN hstgrp ON (hstgrp.groupid = hosts_groups.groupid AND hstgrp.name = 'SEKURIT')
LEFT JOIN host_inventory ON (host_inventory.hostid = hosts.hostid)
LEFT JOIN items i ON (i.hostid = hosts.hostid)
INNER JOIN functions f ON (f.itemid = i.itemid)
INNER JOIN triggers t ON (t.triggerid = f.triggerid AND t.value=1)
#INNER JOIN problem pb ON (pb.objectid = t.triggerid)
#INNER JOIN events e ON (e.objectid = t.triggerid)
#GROUP BY hosts.hostid");
result is
DBD::mysql::st execute failed: Unknown column 'e.name' in 'field list' at test4.pl line 33.
DBD::mysql::st execute failed: Unknown column 'e.name' in 'field list' at test4.pl line 33.
do you have an idea?
In your last post there is only one reference to e in the following comment
#INNER JOIN events e ON (e.objectid = t.triggerid)
probably it is better to use syntax INNER JOIN events as e ON (e.objectid = t.triggerid)
-- more readable
I do not have a database at hand available right at this moment. The following example should work in theory. I have replaced fetch_array on fetch_hashref the output will provide information about each row
ddddd column=value\tcolumn=value\t...
use strict;
use warnings;
my $query = qq(SELECT * FROM tb_test);
db_query($query);
sub db_query {
my $query = shift;
my $count = 1;
my $dbh = DBI->connect(
"DBI:mysql:database=database;host=ip",
"login", 'password',
{'RaiseError' => 1}
);
my $sth = $dbh->prepare($query);
$sth->execute();
while ( my $row = $sth->fetchrow_hashref() ) {
print "%5d ", $count++; # rows count
while( my($k,$v) = each %$row ) { print "%s=%s\t",$k,$v; }
print "\n"; # we done with current row
}
$sth->finish;
$dbh->disconnect;
}
You need look into DBI documentation to get full understanding how it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.