From PHP to Perl


Mysql Interface

Posted in Programming by gosukiwi on November 19, 2006

Well, i will now post the basic differences between PHP and Perl and Mysql

Mysql is a very used sql database, used to store massive ammount of data.

Mysql is supported by alot of free hosting services, like awardspace.com, freewebsitehosting.net, etc

PHP and Perl are also supported by almost all hosting services.

I will show you the basic Mysql Interface with those two languages

That would be connect to a database, select a dabatase, execute queries (insert, delete, update), and numrows.

Connect to a database:

In PHP:

 

mysql_connect(‘host’,’user’,’pass);
mysql_select_db(‘database’)

In Perl:

 

use Mysql;
$db = Mysql->connect(‘host’, ‘database’, ‘user, ‘pass’);

 

Not very hard right? The main difference is that Perl uses a OOP interface to connect, so you will have to access the methods trought $db

 

Executing a query

In PHP:

mysql_query(“SELECT * FROM mytable”);

In Perl:

$db->query(“SELECT * FROM mytable”);

Not much difference, in perl is a little bit shorter, but basically the same.

Select Query

In PHP:

$q = mysql_query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);

In Perl:

my $q = $db->query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);

Still, not much difference, we just assigned that query to a variable.

Display Content

In PHP:

$q = mysql_query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);
while($r = mysql_fetrch_array($q))
{
echo ‘Title: ‘.$r[‘title’].'<br />’.Content: ‘.$r[‘content’];
}

In Perl:

 

 

 

 

$q = $db->query(“SELECT title, content FROM news ORDER BY id DESC LIMIT 3”);
while(%r = $q->fetchhash)
{
print ‘Title: ‘.$r{‘title’}.'<br />Content: ‘.$r{‘content’};
}

Now, in PHP, we use an array with keys and values to hold the data, but in Perl, arrays are only numeric, and we have to use hashes to have a key and value interface. We could have used fetcharray instead of fetchhash but thebn, we would have to use $r[1] instead of $r{‘content’}. That does not happen with PHP, because PHP arrays can have only values with numeric keys, or act like Perl hashes or Python dictionaries.

 

 

 

 

Updating and deleting

In PHP:

$id = $_GET[‘id’];
$uq = mysql_query(“UPDATE news SET content=’$content’ WHERE id=’$id’ LIMIT 1”);
$dq = mysql_query(“DELETE FROM news WHERE id=’$id’ LIMIT 1”);
if($uq && $dq)
{
echo ‘Queries executed successfully’;
}
else
{
echo ‘There has been an error’;
}

In Perl:

use CGI’:all’;
my $id = param(‘id’);
my $uq = $db->query(“UPDATE news SET content=’$content’ WHERE id=’$id’ LIMIT 1”);
my $dq = $db->query(“DELETE FROM news WHERE id=’$id’ LIMIT 1”);
if($uq and $dq)
{
print ‘Queries executed successfully’;
}
else
{
print ‘There has been an error’;
}

Well, not many differences there, you can also use print in php, the ony differences between print and echo in PHP is that print prints only one line, and echo can print multiple lines.

Numbering Rows

In PHP

$q = mysql_query(“SELECT id FROM news”);
$r = mysql_num_rows($q);
print ‘Total Rows: ‘.$r;

 

 

In Perl

my $q = $db->query(“SELECT id FROM news”);
my $r = $q->numrows;
print ‘Total Rows: ‘.$r;

 

 

Not many differences too, in Perl, we refecence directly the query result to use the numrows method, in PHP we dont use a method, just a function, with the query as an argument.

We can also use PHP with an OOP mysql interface, but we have to make a class on our own.

Well, that’s all 😀 I hope it will be useful for you ^^

Advertisements

One Response to 'Mysql Interface'

Subscribe to comments with RSS or TrackBack to 'Mysql Interface'.

  1. syaza said,

    owh.. nice tutorial.

    Can u convert this PERL script to php :

    $sql_1 =qq~select benefit_id from setting_benefit~;

    $sth_1 = $dbh->prepare($sql_1);
    $sth_1->execute() or $sql_error();
    ($ak) = $sth_1->rows();

    for($k=;$kprepare($sql_2);
    $sth_2->execute() or $sql_error();
    ($ab) = $sth_2->fetchrow_array();
    $sth_2->finish();

    push(@suis, $ab);
    }
    $sth_1->finish();
    $common_error(“array:@suis, line:”.__LINE__);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: