Page 1 of 1

Database access with the Perl DBI::DBD modules

Posted: Mon Jun 10, 2002 2:09 pm
by fandomfilms
I'm currently in the process of changing servers and this is the first time I've come across Mysql being set-up off the same server. The company I'm with used Perl to talk back and forth and frankly I have no perl experience. They are suggesting using the following code but when I place it inside my <?php ?> tags it give me a parse error. Can some help me by explaining how this works???

#!/usr/bin/perl
use DBI;
$dsn = "DBI:mysql:database=databasename";
$dbh=DBI->connect($dsn, "username", "password");
$sth = $dbh->prepare("SELECT * FROM table where ID=''5'' ORDER BY date");
$sth->execute;
while( (@results = $sth->fetchrow) != NULL) {
if ($results[2] > 500) {
print "$results[0] - $results[1] - $results[2] - $results[4] - $results[6]\n";
}
}


Thank you in advance...

Posted: Tue Jun 11, 2002 1:58 am
by twigletmac
It won't work in PHP tags because it's perl and therefore a different language. If PHP is setup on the server there's no reason why you can't use PHP to make a connection to MySQL:

Code: Select all

<?php
$user = ''; //your username for the database
$pass = ''; // your password for the database
$db = ''; // the name of your database

@$db_conn = mysql_connect('localhost', $user, $pass) or die(mysql_error());
@mysql_select_db($db) or die(mysql_error());

$sql = "SELECT * FROM table WHERE ID=5 ORDER BY date";
@$result = mysql_query($sql) or die(mysql_error());

while ($info = mysql_fetch_row($result)) &#123;
    if ($results&#1111;2] > 500) &#123;
        $output = implode(' - ', $info);
        echo $output;
    &#125;
&#125;
?>
That's the code roughly translated.

Hope it helps,
Mac