Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pulling pages from a database question... 3

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
First, let me say thanks and hi to Jpadie, Tarwn and all the other PHP Masters that make this the best PHP forum around.

Now, let me get into my question.
My ultimate goal is to build a site and have all the pages loaded into a mysql database except the index.php page. I'm loading them into the database by inserting the html content of the page into a "longtext" data type field. Then what ever link you click on, is performing a sql query to that particular page in the database.
Example: If you click on the "Contact Us" link the select statement says...
select page_name from html_content where page_name = contact us

Well this is working fine and it's very fast like I wanted. here is the problem. If the page that i'm pulling from the data base has php in it, it's not being processed as php.
Is there a way to have this data processed as php then displayed. I've never done this so I don't know if there is a way, or am I spitting into the wind:).

here is a page that I'm calling and it is supposed to just list all the page names out of the database. It works if I copy it to a php page and open it in my browser. but not if I call it from a sql query. All I see is this on the screen...
= $allcategories_columns) { ?>
"); $allcategories_endRow++; } echo(""); }?>


here is the actual page it's pulling

<?php require_once('Connections/testconnect.php'); ?>
<?php
mysql_select_db($database_test, $test);
$query_allcategories = "SELECT * FROM card_catagory";
$allcategories = mysql_query($query_allcategories, $test) or die(mysql_error());
$row_allcategories = mysql_fetch_assoc($allcategories);
$totalRows_allcategories = mysql_num_rows($allcategories);
?>
<table width="713" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="70"><img src="images/bar-small.jpg" width="713" height="8" alt="image header"><br>
<img src="images/headers/categories.jpg" width="713" height="70" align="middle" alt="marketing"></td>
</tr>
<tr>
<td height="27"><img src="images/stripe_bar.jpg" width="713" height="27" alt="postcard templates"></td>
</tr>
<tr>
<td height="375" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td></td>
</tr>
</table>
<table >
<tr>
<?php
$allcategories_endRow = 0;
$allcategories_columns = 3; // number of columns
$allcategories_hloopRow1 = 0; // first row flag
do {
if($allcategories_endRow == 0 && $allcategories_hloopRow1++ != 0) echo "<tr>";
?>
<td height="86"><table width="249" height="77" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="300"><a href="selectthumbs.php?photo_category=<?php echo $row_allcategories['card_catagory_id']; ?>"><?php echo $row_allcategories['card_catagory_desc']; ?></a></td>
</tr>
</table></td>
<?php $allcategories_endRow++;
if($allcategories_endRow >= $allcategories_columns) {
?>
</tr>
<?php
$allcategories_endRow = 0;
}
} while ($row_allcategories = mysql_fetch_assoc($allcategories));
if($allcategories_endRow != 0) {
while ($allcategories_endRow < $allcategories_columns) {
echo("<td> </td>");
$allcategories_endRow++;
}
echo("</tr>");
}?>
</table></td>
</tr>
</table>

<?php
mysql_free_result($allcategories);
?>

 
how is the php included within the db record?
 
I basically create the PHP page in dreamweaver then when it works properly I just copy all from the code window and paste it into the database field. Is that what you mean?
The code from my original post is literally whats loaded into the db record.
 
what i meant was how does the php look in your database?
e.g.
Code:
Hello $name, how are you?
or
Code:
Hello <?=$name?>, how are you?
 
I think I use a little of both. the long code blurb above is an actual page straight out of the database.
 
i agree with sleipnir214. use eval()

Code:
eval ("echo $row['databasecontent'];");
 
Thanks Guys, I'll try it and let you know if it works.
 
Hi guys, I'm revisiting this issue. I'm not going to be able to avoid it.
Basically I'm trying to create a site and all the pages are loaded into a database and are displayed as the result of a sql query.
I got advice to use the Eval() function, but have found that it is synomous with Evil() and have used at least 20 tutorials I found and haven't been able to output one single piece of code onto the screen.
Can you guys steer me toward any other possible solutions to pulling PHP out of a database.
I found an interesting quote on this board (now that the search function is working)

Quote (Rasmus Lerdorf):
If eval() is the answer, you’re almost certainly asking the wrong question.

Please help

Thanks.
 
I made a couple of tests, just to be sure on how eval works, and i found that:

When using eval(), you cannot have opening and closing PHP tags in the code to eval'ed. or you'll get an error.

So this for example:

Code:
[red]
<?PHP
some php code here...
?>
[/red]

will throw out an error like: Parse Error, Sytax error unexpected '<' .... from the opening PHP tag. If you are going to use eval, you need to remove all PHP tags inside your page code. so you'll need to turn everything that is not inside PHP tags, into echo statements.

so for example if you have your page that starts

Code:
<html>
<head>.....


you going to have to turn it into
Code:
echo <<<EEE
<html>
<head>
...

EEE;




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
eval() is fine so long as it is you that control what goes into the eval string, and not part of the user generated content.

eval assumes you start in php mode. if this is not the case then adding a pair of tags to the beginning of the text to evaluate should fix the problem.

Code:
eval ("?>".$row['databasecontent']");

i can't think of another way of doing things that meets your criteria other than:
Code:
//grab a unique temporary name
$t = tempnam('/tmp', 'temp');
//create a file for writing
$fh = fopen($t, "wb");
//write the relevant column from the db to the new file
fwrite($fh, $row['databaseoutput'];
//close the file
fclose($fh);
//start some output buffering
ob_start();
//include the temp file.  include will evaluate the file
include $t;
//grab the contents of the output buffer to a variable
$contents = ob_get_contents();
//dissolve the output buffer cleanly
ob_end_clean();
//delete the temporary file
unlink ($t);

or
Code:
$contents = shell_exec('php -r "?>'.$row['databasecontents'].'"');

but i can't see any benefit of either over eval()
 
Thanks guys, but I guess i'm not being clear or your answers are over my head.

My over all goal is to build a website that has one database and one page called index.php. what ever link you click on executes a database query that pulls content from the database and displays it in the center of the page.

By the way, I have a few pages that only have HTML in them and they work great. As soon as I put PHP on the page it dies.


(I've scaled the code back to make it more readable)

example of index.php code.

<?php require_once('Connections/database.php'); ?>


<?php
mysql_select_db($database_database, $database);
$query_Recordset1 = "SELECT * FROM html_content WHERE page_name = 'general_info'";
$Recordset1 = mysql_query($query_Recordset1, $database) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

//here is where the content of the database will be displayed.
echo $row_Recordset1['page_content'];

mysql_free_result($Recordset1);
?>

example of the code I loaded into the database in the content field. This is straight from the php.net manual in the eval() page.

//I'm just using this for testing
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>

this is what should be displayed on the screen:
This is a $string with my $name in it.
This is a cup with my coffee in it.


I've tried removing the php tags and I get this on the screen...all on one row.
$string = 'cup'; $name = 'coffee'; $str = 'This is a $string with my $name in it.'; echo $str. "\n"; eval("\$str = \"$str\";"); echo $str. "\n";

when I leave the php tags in the screen is blank.
Even when I copy the examples straight from the manual it still doesn't work for me. I'm sure once I can get even one thing to display on the screen I'll understand it.

I know this is a lot ask, but can someone write a small script that has I could copy and paste into my database field "page_content" and then do a query for it and just echo it on the screen.

I would be for ever greatful.



 
first off read up on ues of quotes and expansion of variables within single quotes.
 
To expand on jpadie's suggestion:
variables inside single quotes are not replaced by their values automatically. while variables inside double quotes are.

So this:

$string='This is a $string with my $name in it.';
will not replace the variables with their values, however this will:
$string=[red]"[/red]This is a $string with my $name in it.[red]"[/red];

Also, you are using eval, but then do nothing with its output.

Try:
Code:
echo eval($str);

to give you an example copy the following into a row in your DB, and then do the second part from your index.php


Code:
$firstvariable="coffee";
$secondvariable="cup";

echo "This is a string of text with a $firstvariable $secondvariable in the middle";


then in index.php

Code:
$results=eval($row['fieldname']);
echo $results;
Your field contents should NOT contain PHP delimiters:
[red]<?PHP ?>[/red]

If done correctly:
You should get:
This is a string of text with a coffee cup in the middle



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
OH MY GOD.... IT WORKED.
I feel like I just won the lottery. I have running into a brick wall on this for almost a month. I finally understand what I was doing wrong through you examples.
I can't tell you guys how much I appreciate your help.
This is great.

thanks Jpadie and Vacunita
I hope I can return the favor one day.
 
Glad to see it worked out.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

sd0t1 said:
thanks Jpadie and Vacunita
I hope I can return the favor one day.
Why just in words and why in the future ? You can reward them in Tek-Tips style and now.

There are links below their each message, you only have to click them :

* [navy]Thank ???
for this valuable post![/navy]

Thanks link's title said:
This link will mark this post as valuable by putting a star next to it. It will cast a vote for ??? for TipMaster of the Week.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top