Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql array 1

Status
Not open for further replies.

ZimZangZoom

Programmer
Dec 17, 2004
26
Hello,

I am trying to get some info out of my MySql Database and would like to use the Array later on in the script..

I wish to loop through the array using "foreach" or "while".

In the array i have 4 elements
-name
-email
-time
-day

how can i slit up my array?

I have tried,

foreach $array (@array) {
print "$array";
}

but i need to devide up the results the mysql query gave me.

Drew
 
Code:
use CGI;

my $q=new CGI;

use DBI;
require "db_config.pl";

my $dbh = DBI->connect($conf::db, $conf::username, $conf::password) or die "Couldn't connect to database: " . DBI->errstr;
my $sth= $dbh->prepare('select name, email, time, day from the_table');

while (($name, $email, $time, $day)=$sth->fetchrow_array) {
# do whatever
   push(@names,  $name);   #array for use later on
   push(@emails, $email);  #array for use later on
   push(@times,  $time);   #array for use later on
   push(@days,   $day);    #array for use later on
}
db_config.pl
Code:
#usr/bin/perl

      $db="DBI:mysql:tablename:localhost";
      $username="user";
      $password="password";
      
      1;

--Paul

cigless ...
 
Newbie here, pardon me for butting in...

Paul, what's the purpose of this line?

my $q=new CGI;

 
just a hang on from a previous script

CGI is the Common Gatewaty Interface, and Lincoln D. Stein wrote a nice module to hasndle all the requisites to talk to webservers

google for CGI and LDS(Lincoln D Stein)

HTH
--Paul

cigless ...
 
Yes of course, and I understand the 'use CGI' declaration, but I frequently see that statement (with different variable names) immediately following 'use CGI' and was wondering what the point was. Is it used to localize all default variables, or something like that?
 
It creates a new instance of the CGI object so that variables passed through the CGI object can be referenced

Hope that makes sense
--Paul

cigless ...
 
If by variables you mean client browser input and by referenced you mean indexed (i.e. []), then yes I follow.

If not, can you suggest some reference material I can study?
I've got 'Programming Perl' and 'The Perl Cookbook' but as yet haven't run across a statement used that way.


 
Not necessarily just browser input, cookies as well, Google for CGI, and Lincoln D. Stein, the top of the list should be either his page at Cold Springs Harbor Lab, or his page on cpan,
$name=$q->param('name');
is an example of how to reference the form variable name in your script. You don't have to use that format though, it's the same as
$name =param('name');

for reference material, perlmonks.org, also in the FAQ section here there's a list of online, and deadtree resources on learning perl (which originally appeared at devshed.com, another good resource)

HTH
--Paul

cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top