编程perl语言,详细解释一下下面的代码,我想把下面的代码转换成php
#!/usr/bin/perl#TheSQLdatabaseneedstohaveatablecalledhighscores#thatlookssomethinglik...
#!/usr/bin/perl
# The SQL database needs to have a table called highscores
# that looks something like this:
#
# CREATE TABLE highscores (
# game varchar(255) NOT NULL,
# player varchar(255) NOT NULL,
# score integer NOT NULL# );
#use strict;use CGI;use DBI;
# Read form data etc.my $cgi = new CGI;
# The results from the high score script will be in plain textprint
$cgi->header("text/plain");
my $game = $cgi->param('game');
my $playerName = $cgi->param('playerName');
my $score = $cgi->param('score');
exit 0 unless $game;
# This parameter is required
# Connect to a databasemy
$dbh = DBI->connect( 'DBI:mysql:databasename', 'username', 'password' ) || die "Could not connect to database: $DBI::errstr";
# Insert the player score if there are anyif( $playerName && $score) {
$dbh->do( "insert into highscores (game, player, score) values(?,?,?)", undef, $game, $playerName, $score );
}
# Fetch the high scoresmy
$sth = $dbh->prepare( 'SELECT player, score FROM highscores WHERE game=? ORDER BY score desc LIMIT 10' );
$sth->execute($game);
while (my $r = $sth->fetchrow_arrayref) {
print join(':',@$r),"\n"
} 展开
# The SQL database needs to have a table called highscores
# that looks something like this:
#
# CREATE TABLE highscores (
# game varchar(255) NOT NULL,
# player varchar(255) NOT NULL,
# score integer NOT NULL# );
#use strict;use CGI;use DBI;
# Read form data etc.my $cgi = new CGI;
# The results from the high score script will be in plain textprint
$cgi->header("text/plain");
my $game = $cgi->param('game');
my $playerName = $cgi->param('playerName');
my $score = $cgi->param('score');
exit 0 unless $game;
# This parameter is required
# Connect to a databasemy
$dbh = DBI->connect( 'DBI:mysql:databasename', 'username', 'password' ) || die "Could not connect to database: $DBI::errstr";
# Insert the player score if there are anyif( $playerName && $score) {
$dbh->do( "insert into highscores (game, player, score) values(?,?,?)", undef, $game, $playerName, $score );
}
# Fetch the high scoresmy
$sth = $dbh->prepare( 'SELECT player, score FROM highscores WHERE game=? ORDER BY score desc LIMIT 10' );
$sth->execute($game);
while (my $r = $sth->fetchrow_arrayref) {
print join(':',@$r),"\n"
} 展开
3个回答
展开全部
$connect=mysql_connect($host,$user,$password) || die "Could not connect to database:mysql_error";
mysql_select_db($db[,$connect]); ip,用户,密码,数据库名自己去填
$query="insert into highscores (game, player, score) values(?,?,?)";
mysql_query($query);
$query1="SELECT player, score FROM highscores WHERE game=? ORDER BY score desc LIMIT 10";
$result=mysql_fetch_array(mysql_query($query1),MYSQL_ASSOC);
mysql_close($connect);
for($key=0;$key<count($result);$key++){
echo $a[$key];}
mysql_select_db($db[,$connect]); ip,用户,密码,数据库名自己去填
$query="insert into highscores (game, player, score) values(?,?,?)";
mysql_query($query);
$query1="SELECT player, score FROM highscores WHERE game=? ORDER BY score desc LIMIT 10";
$result=mysql_fetch_array(mysql_query($query1),MYSQL_ASSOC);
mysql_close($connect);
for($key=0;$key<count($result);$key++){
echo $a[$key];}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询