[Solved] Retrieve and execute PHP code from a Database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

[Solved] Retrieve and execute PHP code from a Database.

Post by bluenote »

Hi there,

i have posted this question some days ago, but maybe i did not explain the problem exactly enough ...

As the result of a database query, i have two strings - the first one contains normal text (like 'Hello, World') and the second one contains PHP code which should execute a second query over another database table. The result of the second query should be inserted in the 'Hello, World' text string of the first one.

So how can i execute the PHP code (result of the first query) and insert the result in the text string and echo it afterwards?

I tried to work around with eval(), but the result was

Code: Select all

їThu Apr 15 17:14:12 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(130) : eval()'d code on line 1
їThu Apr 15 17:15:09 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(130) : eval()'d code on line 1
їThu Apr 15 17:22:00 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(130) : eval()'d code on line 1
їThu Apr 15 17:24:16 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(131) : eval()'d code on line 1
їThu Apr 15 17:27:13 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(130) : eval()'d code on line 1
їThu Apr 15 17:29:56 2004] їerror] PHP Parse error:  parse error in /export/home/local/Web/include/templates/content/c_einrichtungen.inc(133) : eval()'d code on line 1
Anyone an idea :idea: ?

Greez,
- bluenote
Last edited by bluenote on Wed Aug 11, 2004 1:33 am, edited 1 time in total.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

It sounds like eval() should work for you, but it would certainly be touchy. How about posting the text that you are passing to eval that is causing the error?
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Post by bluenote »

Hi Dave,

at first, thanx for your reply... then, here's the first query:

Code: Select all

<?php

$chapter = $_REQUEST["chapter"];
$siteID = $_REQUEST["siteID"];

$rquery = "SELECT DBgaps.gaps_departments.* from DBgaps.gaps_departments WHERE DBgaps.gaps_departments.dep_g03 = '$department' AND DBgaps.gaps_departments.dep_g04 = '$chapter' AND DBgaps.gaps_departments.dep_g07 = '$siteID' ORDER BY DBgaps.gaps_departments.dep_g08";

$rerg = MYSQL_QUERY($rquery);

$rnumrows = MYSQL_NUM_ROWS($rerg);

if ($rnumrows > '0'){
	
	$r = 0;
	
	$dep_g03 = mysql_result($rerg,$r,"dep_g03");
	$dep_g04 = mysql_result($rerg,$r,"dep_g04");
	$dep_g05 = mysql_result($rerg,$r,"dep_g05");
	$dep_g06 = mysql_result($rerg,$r,"dep_g06");
			$dep_g07 = mysql_result($rerg,$r,"dep_g07");
			$dep_g08 = mysql_result($rerg,$r,"dep_g08");
			$dep_g09 = mysql_result($rerg,$r,"dep_g09");
			$dep_g10 = mysql_result($rerg,$r,"dep_g10");
			$dep_g11 = mysql_result($rerg,$r,"dep_g11");
			$dep_g12 = mysql_result($rerg,$r,"dep_g12");
			$dep_g13 = mysql_result($rerg,$r,"dep_g13");
			$dep_g14 = mysql_result($rerg,$r,"dep_g14");
			$dep_g15 = mysql_result($rerg,$r,"dep_g15");
			$dep_g16 = mysql_result($rerg,$r,"dep_g16");
			$dep_g17 = mysql_result($rerg,$r,"dep_g17");
			$dep_g18 = mysql_result($rerg,$r,"dep_g18");
			$dep_g19 = mysql_result($rerg,$r,"dep_g19");
			$dep_g20 = mysql_result($rerg,$r,"dep_g20");
			$dep_g21 = mysql_result($rerg,$r,"dep_g21");
			$dep_g22 = mysql_result($rerg,$r,"dep_g22");
			$dep_g23 = mysql_result($rerg,$r,"dep_g23");
			$dep_g24 = mysql_result($rerg,$r,"dep_g24");
			$dep_g25 = mysql_result($rerg,$r,"dep_g25");
			$dep_g26 = mysql_result($rerg,$r,"dep_g26");
			$dep_g27 = mysql_result($rerg,$r,"dep_g27");
			$dep_g28 = mysql_result($rerg,$r,"dep_g28");
			
			$access = mysql_result($rerg,$r,"access");
			$code = mysql_result($rerg,$r,"code");
			
			$dep_g10 = strtr($dep_g10, $specialchars);
			$dep_g11 = strtr($dep_g11, $specialchars);
			$dep_g27 = strtr($dep_g27, $specialchars);
			$dep_g28 = strtr($dep_g28, $specialchars);
			
			$loop_count = $rnumrows - 1;}

else {}

?>
The text is stored in $dep_g10 (english version) and $dep_g11(german version); the code for the second query is stored in $code:

Code: Select all

<?php

$hl1query = "SELECT DBlib.libjournals.id, COUNT(DBlib.libjournals.id) FROM DBlib.libjournals WHERE DBlib.libjournals.l03='A' AND DBlib.libjournals.l08='yes' AND DBlib.libjournals.l33='yes' GROUP BY DBlib.libjournals.id";

$hl1erg = MYSQL_QUERY($hl1query);

$hl1numrows = MYSQL_NUM_ROWS($hl1erg);

if ($hl1numrows > '0'){
	
	$hl1 = 0;
	
	$id = mysql_result($hl1erg,$hl1,"id");}
		
		else {}
?>
The (english) text in $dep_g10 sounds like this: "Currently, there are $h1numrows articles stored; to retrieve one article, blah blah ..."

For further explanation: The first query ($rquery) pulls content for a specific department, a specific chapter and a specific page from the db to fill a template. Each page is partioned in stanzas, whereof the template offers 30 different subtypes (i. e. 'text_plain', 'text_image', 'link_extern' etc.). These subtypes are also defined in the database and stored in $dep_g09. So if the department is '14', the chapter is '001' and the siteID is '3', $rquery will extract a certain number of rows (each row is 1 stanza) and fill the template.

Because of this template is used for 30 different departments, i can't include() or require() department-specific code in one stanza type.

- bluenote
Post Reply