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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl dbi equivalent of php row identifier

Status
Not open for further replies.

mwpclark

Programmer
Mar 14, 2005
59
US
I am looking for a way to identify mysql row fields in perl, similar to php. With the following perl code, when I modify it for another table with different fields, I have to re-count the fields. With the php, I just have to comment out unused fields or add new ones, because they are identified by name not number.

perl
Code:
$dsn = "dbi:mysql:database";
$dbh = DBI->connect($dsn,'user','password!');
$data = $dbh->selectall_arrayref("SELECT * FROM table") or die "SQL error";
$records = @{$data};
for($x=0;$x<$records;$x++){
$id = $data -> [$x][0];
$first = $data -> [$x][1];
$middle = $data -> [$x][2];
$name = $data -> [$x][3];

php
Code:
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
$row = mysql_fetch_array($result);
$id = $row['id'];
$first = $row['first'];
$middle = $row['middle'];
$name = $row['name'];

Thanks for suggestions
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top