Jump to content

Changing a perl script to PHP


PHILIPS
 Share

Recommended Posts

hello everyone,I have been stuck on this assignment for ages and would be very gratefull if anybody could help me or point me in the right direction. I have been trying for ages but not having much luck. Thank you for any help received.#!/usr/bin/perl -wuse CGI':standard';use CGI::Carp "fatalsToBrowser";use DBI;sub err(){print "Error in @_[0] field - @_[1]";print "</BODY> <BODY BGCOLOR=\"white\">";exit;}print header;print "<HTML> <BODY BGCOLOR=\"white\">";$PRICE=param('PRICE');$BEDROOMS=param('BEDROOMS');$COMMENTS=param('COMMENTS');$LOCATION=param('LOCATION');$PROPTYPE=param('PROPTYPE');$NAME=param('NAME');$TELEPHONE=param('TELEPHONE');$EMAIL=param('EMAIL');if(($PRICE !~ /\A\d+\Z/) || ($PRICE>1000000 || $PRICE<0)){&err("PRICE",$PRICE);}if(($BEDROOMS !~ m/\A\d+\Z/) || ($BEDROOMS>10 || $BEDROOMS<1)){&err("BEDROOMS",$BEDROOMS);}if($COMMENTS !~ m/\w|/){&err("COMMENTS",$COMMENTS);}if($LOCATION !~ m/London|Manchester|Coventry|Leeds/i ){&err("LOCATION",$LOCATION);}if($PROPTYPE !~ m/Freehold|Leasehold/i ){&err("PROPTYPE",$PROPTYPE);}if($NAME !~/\A[a-z]+\Z/i){&err("NAME",$NAME);}if($TELEPHONE !~ m/\A\d+\Z/i){&err("TELEPHONE",$TELEPHONE);}if($EMAIL !~/\A[a-z]+\@[a-z]+\Z/i){&err("EMAIL",$EMAIL);}if($PROPTYPE eq "FREEHOLD"){$PROPTYPE="F"};if($PROPTYPE eq "LEASEHOLD"){$PROPTYPE="L"};print "<H1> Search Matches <H1>";$query=qq(SELECT * FROM houses where BEDROOMS>='$BEDROOMS' AND PRICE<='$PRICE' AND LOCATION='$LOCATION' AND PROPTYPE='$PROPTYPE');$user='';$pass='';$dbh=DBI->connect('DBI:mysql:database=staffkeithy;host=possum',$user,$pass) or die "Can't connect to database";$sth=$dbh->prepare($query);$sth->execute;if($sth->rows>0){print "<table border=\"1\"> ";print "<tr> <td> Price </td> <td> Bedrooms</td> <td>Comments </td> </tr>";while(@result=$sth->fetchrow_array){print " <tr> <td> £ $result[0] </td> <td> $result[1] </td> <td> $result[2] </td> \n";}print "</table>";}else{print "No Searches found\n";}$sth->finish;$dbh->disconnect;

Link to comment
Share on other sites

For the error sub, the main difference is that in PHP a sub is a function.

function err($name, $val){  print "Error in {$name} field - {$val}";  print "</BODY> <BODY BGCOLOR=\"white\">"; //why do you do this?  exit();}

For the "print header" line, you can just leave that out, PHP will send the headers automatically. For the lines like this:$PRICE=param('PRICE');You will need to use either $_GET or $_POST, depending on how this is set up. I don't see a form anywhere, but this looks like a script to handle a database search, so it will probably use POST. So the equivalent of the line above would be this:$PRICE = $_POST['PRICE'];The validation lines are all using regular expressions, so they look a little confusing. But in reality it's not that bad.

//if(($PRICE !~ /\A\d+\Z/) || ($PRICE>1000000 || $PRICE<0)){&err("PRICE",$PRICE);}if(!is_numeric($PRICE) || ($PRICE>1000000 || $PRICE<0)) { err("PRICE",$PRICE); }//if(($BEDROOMS !~ m/\A\d+\Z/) || ($BEDROOMS>10 || $BEDROOMS<1)){&err("BEDROOMS",$BEDROOMS);}if(!is_numeric($BEDROOMS) || ($BEDROOMS>10 || $BEDROOMS<1)) { err("BEDROOMS",$BEDROOMS); }//if($COMMENTS !~ m/\w|/){&err("COMMENTS",$COMMENTS);}if($COMMENTS == "") { err("COMMENTS",$COMMENTS); }//if($LOCATION !~ m/London|Manchester|Coventry|Leeds/i ){&err("LOCATION",$LOCATION);}if(!(!strcasecmp($LOCATION, "London") || !strcasecmp($LOCATION, "Manchester") || !strcasecmp($LOCATION, "Coventry") || !strcasecmp($LOCATION, "Leeds"))) { err("LOCATION",$LOCATION); }//if($PROPTYPE !~ m/Freehold|Leasehold/i ){&err("PROPTYPE",$PROPTYPE);}if(!(!strcasecmp($PROPTYPE, "FREEHOLD") || !strcasecmp($PROTOTYPE, "LEASEHOLD"))) { err("PROPTYPE",$PROPTYPE); }//if($NAME !~/\A[a-z]+\Z/i) {&err("NAME",$NAME);}if(!preg_match("/\\A[a-z]+\\Z/i", $NAME) { err("NAME",$NAME); }//if($TELEPHONE !~ m/\A\d+\Z/i) {&err("TELEPHONE",$TELEPHONE);}if(!is_numeric($TELEPHONE)) { err("TELEPHONE",$TELEPHONE); }//if($EMAIL !~/\A[a-z]+\@[a-z]+\Z/i){&err("EMAIL",$EMAIL);}if(!preg_match("/\\A[a-z]+\\@[a-z]+\\Z/i", $EMAIL) { err("EMAIL",$EMAIL); }

These type of lines are pretty straightforward:if($PROPTYPE eq "FREEHOLD"){$PROPTYPE="F"};becomesif($PROPTYPE == "FREEHOLD"){$PROPTYPE="F"};The database works a little differently. But since it's using MySQL it would look like this:old:

$query=qq(SELECT * FROM houses where BEDROOMS>='$BEDROOMS' AND PRICE<='$PRICE' AND LOCATION='$LOCATION' AND PROPTYPE='$PROPTYPE');$user='';$pass='';$dbh=DBI->connect('DBI:mysql:database=staffkeithy;host=possum',$user,$pass) or die "Can't connect to database";$sth=$dbh->prepare($query);$sth->execute;

new:

$query="SELECT * FROM houses where BEDROOMS>='" . intval($BEDROOMS) . "' AND PRICE<='" . intval($PRICE) . "' AND LOCATION='$LOCATION' AND PROPTYPE='$PROPTYPE'";$user='';$pass='';mysql_connect("possum",$user,$pass) or die ("Can't connect to database");mysql_select_db("staffkeithy");$sth = mysql_query($query);

Since $sth is now a result set instead of an object, this line:if($sth->rows>0)changes to this:if(mysql_num_rows($sth) > 0)And then this:while(@result=$sth->fetchrow_array){becomes this:while($result = mysql_fetch_array($sth)){And then it would be better, although not necessary, to change this:print " <tr> <td> £ $result[0] </td> <td> $result[1] </td> <td> $result[2] </td> \n";to this:print " <tr> <td> £ {$result[0]} </td> <td> {$result[1]} </td> <td> {$result[2]} </td> \n";And then these last two lines:$sth->finish;$dbh->disconnect;You can pretty much leave out because PHP will do garbage collection on the database automatically, but if you want you can throw in this:mysql_free_result($sth);mysql_close();But, like I said, if the script ends PHP will automatically do that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...