in reply to Re^2: Help with MySQL SELECT into multidimensional array
in thread Help with MySQL SELECT into multidimensional array
See some untested code below... Once you get connected and have a database handle, the sequence is : prepare query ->execute query -> retrieve results. There are several "flavors" of result retrieval. I show an easy way below.
I recommend the following book, what DB are you using?
Programming the Perl DBI, Database programming with Perl
By Tim Bunce, Alligator Descartes.
Update: added placeholder comment in code above. Also just tested a very similar query on my local MySQL installation. See if you can get this first part working.use DBI; my $database = 'somedatabase'; #your system specific my $hostname = 'localhost'; ##your system specific my $port = 3306; #default mysql port my $user = 'someuser'; #your system specific my $password = 'somepassword'; #your system specific my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" or die "DB data set name failed $!\n"; my $dbh = DBI->connect($dsn, $user, $password, {RaiseError => 1}) or die "DB connect failed $!\n"; my $query = $dbh->prepare("SELECT c2.id, c2.name as 'client' FROM client c2 WHERE level = 50 and status = 1"); $query->execute(); while (my ($id, $name) = $query->fetchrow_array) { print "$id \t $name\n"; } __END__ To make a parameter a variable, use a placeholder: my $query = $dbh->prepare("SELECT c2.id, c2.name as 'client' FROM client c2 WHERE level = ? and status = ?"); $query->execute(50,1);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Help with MySQL SELECT into multidimensional array
by btongeorge (Initiate) on Dec 02, 2011 at 15:27 UTC | |
by Marshall (Canon) on Dec 02, 2011 at 15:50 UTC | |
by btongeorge (Initiate) on Dec 02, 2011 at 15:57 UTC | |
by Marshall (Canon) on Dec 02, 2011 at 16:18 UTC | |
by btongeorge (Initiate) on Dec 02, 2011 at 16:54 UTC | |
| |
|
Re^4: Help with MySQL SELECT into multidimensional array
by btongeorge (Initiate) on Dec 02, 2011 at 14:32 UTC | |
by Marshall (Canon) on Dec 02, 2011 at 15:13 UTC |