tags:

views:

123

answers:

4

I am doing the following, and getting "1" which I assume means the statement wend well. But I would like the result instead.

What's wrong?

#!/usr/bin/perl

use strict;
use DBI;

my $host = "test";
my $database = "dd";
my $port = 3306;
my $user = "uuu";
my $pw = "ppp";

my $mysql = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $pw)
    or die "Cannot connect to MySQL server\n";

my $m = $mysql->do(qq{select MAX(idvisit) from log_visit});

print $m;
+3  A: 

do returns the number of affected rows. You might want to look into the statement class and specifically, the execute function.

Femaref
It I change `do` to `execute` I get `Can't locate object method "execute" via package "DBI::db"`.
Sandra Schlichting
I meant the statement class of course. the same you used.
Femaref
+3  A: 
my $m = $mysql->prepare("select MAX(idvisit) from log_visit");
$m->execute() or die "Couldn't execute statement: ".$m->errstr;
print $m->fetch()->[0];
Sandra Schlichting
+7  A: 

It's always worth checking the documentation for functions that you're having trouble with.

In this case the DBI documentation for "do" says:

Prepare and execute a single statement. Returns the number of rows affected or undef on error.

And, more explicitly,

It should not be used for SELECT statements because it does not return a statement handle (so you can't fetch any data).

davorg
+1; anyone doing DB work needs to read through the [*full* documentation for DBI](http://perldoc.perl.org/DBI.html) (`perldoc DBI`) so they know what facilities exist.
Ether
+1  A: 
my $m = $mysql->selectrow_array(qq{select MAX(idvisit) from log_visit});
mscha