| 1 |
|
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>Embperl Examples - DBI access using Loops</title> |
| 5 |
</head> |
| 6 |
<body background="../images/jazzbkgd.gif"> |
| 7 |
|
| 8 |
<h1>Embperl Examples - DBI access using Loops</h1> |
| 9 |
<hr><b>NOTE:</b><br> |
| 10 |
You must set the database and table to something which exists on your system<br> |
| 11 |
Maybe it's necessary to insert a PerlModule DBI into your srm.conf to get this working<br> |
| 12 |
|
| 13 |
<hr> |
| 14 |
|
| 15 |
[- |
| 16 |
|
| 17 |
$DSN = 'dbi:mysql:test' ; |
| 18 |
$table = 'dbixrs1' ; |
| 19 |
|
| 20 |
use DBI ; |
| 21 |
|
| 22 |
# connect to database |
| 23 |
$dbh = DBI->connect($DSN) or die "Cannot connect to '$DSN'" ; |
| 24 |
|
| 25 |
# prepare the sql select |
| 26 |
$sth = $dbh -> prepare ("SELECT * from $table") or die "Cannot SELECT from '$table'" ; |
| 27 |
|
| 28 |
# excute the query |
| 29 |
$sth -> execute or die "Cannot execute SELECT from '$table'"; |
| 30 |
|
| 31 |
# get the fieldnames for the heading in $head |
| 32 |
$head = $sth -> {NAME} ; |
| 33 |
|
| 34 |
-] |
| 35 |
|
| 36 |
<table border=1> |
| 37 |
|
| 38 |
<tr> |
| 39 |
[$ foreach $h @$head $] |
| 40 |
<th>[+ $h +]</th> |
| 41 |
[$ endforeach $] |
| 42 |
</tr> |
| 43 |
|
| 44 |
[$ while $dat = $sth -> fetchrow_arrayref $] |
| 45 |
<tr> |
| 46 |
[$ foreach $v @$dat $] |
| 47 |
<td>[+ $v +]</td> |
| 48 |
[$ endforeach $] |
| 49 |
</tr> |
| 50 |
[$ endwhile $] |
| 51 |
</table> |
| 52 |
|
| 53 |
<p><hr> |
| 54 |
|
| 55 |
<small>HTML::Embperl (c) 1997-1998 G.Richter</small> |
| 56 |
|
| 57 |
|
| 58 |
</body> |
| 59 |
</html> |