我想从PHP应用程序中使用Perl库.我听说有可能.
我已经考虑过用PHP重写库的可能性,但我认为这不是一个好主意,因为它很难.
我在php(index.php)中有两个页面,在Perl(dbcon.pl)中有另一个页面.
基本上我希望我的php文件只显示UI,所有数据操作都将在Perl文件中完成.
我试过index.pl
<?php include("dbcon.pl");?>
<html>
<br/>PHP</br>
</html>
Run Code Online (Sandbox Code Playgroud)
和dbcon.pl有
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use CGI::Simple;
my $cgi = CGI::Simple->new;
my $dsn = sprintf('DBI:mysql:database=%s;host=%s','dbname','localhost');
my $dbh = DBI->connect($dsn,root =>'',{AutoCommit => 0,RaisError=> 0});
my $sql= "SELECT * FROM products";
my $sth =$dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
while (my @row = $sth->fetchrow_array){
print $cgi->header, <<html;
<div> @row[0] @row[1] @row[2] @row[3] @row[4]</div>
html
}
Run Code Online (Sandbox Code Playgroud)
但是当我在浏览器中运行index.php时,它会打印dbcon.pl文件中的所有代码而不是执行它
如何克服这个问题?
注意:我在Windows环境中运行它
还有其他办法吗?