Once You Know, You Newegg

mySQL get data into arrays

Okay, say you have a database with some banner data. This was used for a PHP script but, I have not tested in Perl.
The placement there is a column `placement` which could contain data like sideadtop, sideadmiddle, sideadbottom or any position info.
Below will get each found `placement` into its’ own array.

$result = @mysql_query(“SELECT url, image, placement FROM advert WHERE user=’1′ ORDER BY placement”);
$i = 0;
while($process = @mysql_fetch_assoc($result)){
$row[$i] = $process;
$placement = $row[$1][‘placement’];
$$placement[‘url’] = $row[$1][‘url’];
$$placement[‘image’] = $row[$1][‘image’];
$i++;
}

so now we have the three arrays
$sideadtop,$sideadmiddle,$sideadbottom
with contents url and image
sorted by placement alphabetically ascending


Here is another way to get data into rows:
############################### get lines into individual arrays ###################################
#This approach stores the results of a SQL call in an array. In this example,
#the query is calling for rows with three columns of data, and printing each
#row

use DBI;

$dbh = DBI->connect(‘DBI:mysql:[DATABASE]‘, ‘[USER]‘, ‘[PASSWORD]‘)
|| die “ERROR: $DBI::errstr”;

$query = “[SQL QUERY GOES HERE]“;

$sth = $dbh->prepare($query);

$sth->execute();

$data = $sth->fetchall_arrayref();
$sth->finish;

foreach $data ( @$data) {

($variable1, $variable2, $variable3) = @$data;

print “$variable1\n”;
print “$variable2\n”;
print “$variable3\n”;

}

$dbh->disconnect(

Comments are closed.