Page 1 of 1

How to do this ASP in PHP - utter newbie.

Posted: Fri Aug 09, 2002 1:51 am
by codestorm
Hopefully some here know both ASP and PHP. I'd like to know if it's possible to do something akin to the code below in PHP.

---ASP Page---
<%@enablesessionstate="false" language="javascript"%>
<html>
<head>
<title>Execute Database Stored Code Test</title>
</head>
<body>
<script runat="server" language="javascript">
var str_TestVar = "How are you?"
var lng_CodeID = Request.QueryString("CodeID");
if (!isNaN(lng_CodeID)){
var obj_Connection = Server.CreateObject("ADODB.Connection");
obj_Connection.Open("driver={SQL Server};server=hrwbausdev1;database=test1;uid=sa;pwd=<sa pwd>");
var obj_Recordset = obj_Connection.Execute("select C_Body from A_Code where C_CodeID=" + lng_CodeID);
if (!(obj_Recordset.BOF && obj_Recordset.EOF))
eval(obj_Recordset.Fields("C_Body").Value);
obj_Recordset = null;
obj_Connection = null;
}
</script>
</body>
</html>
---SQL Server Script---
create database test1
use test1
create table A_Code (C_CodeID int identity(1,1) not null primary key, C_Body varchar(8000) not null)
insert A_Code (C_Body) values ('Response.write(''Hello World. '' + str_TestVar)')
---end---

In essence this is just reading a string from a database field which happens to be executable code, and dynamically executing in within the page. Can it be done in PHP?

PS I've just started learning PHP and have yet to actually code anything in it. exec() initially sounded like the go, but not sure if it can execute a string as well as a file?

Incidentally, just signed up for these forums, haven't looked around much yet, but is there a search function to look for past threads on the topic?

codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
You're not a complete programmer unless you know how to guess.
I hope I never consider myself an 'expert'.
<insert witticism here>

Posted: Fri Aug 09, 2002 2:37 am
by twigletmac
Something that may help is asp2php which does a fairly good job of converting ASP to PHP.

There's a link to the search facility underneath the bit that says 'PHP Forums - The PHPDN United Forums' at the top of the page.

Mac

Thanks, semi-solved...

Posted: Fri Aug 09, 2002 5:38 am
by codestorm
I'll check out the converter in detail soon - sounds quite interesting as a learning aid.

Someone on another forum pointed me in the right direction I think with the eval function. Didn't even occur to me to check if PHP had a f'n of the same name which - lo and behold - performs roughly the same function.

Now, off to actually learn PHP.

:wink: