Forum Moderators: coopster

Message Too Old, No Replies

using EVAL won't work

How to use eval to get php code OUT of a database

         

Jord

8:39 am on Oct 21, 2010 (gmt 0)

10+ Year Member



Hi, I just learned how to get php code to work when gotten out a database. It's just not working for me, I'm probably doing something terrible wrong and I hope you guys can help me.
<?php
//con has been made already
$sql = "SELECT * FROM sidebar WHERE id = '1' ORDER BY id DESC limit 1";
$query = mysql_query($sql) or die("".mysql_error());
while( $show = mysql_fetch_object( $query ) ) {


eval("?>" . $show->content. "<?");
} ?>



Where content is this:
<?
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);


$sql = "SELECT * FROM artikelen limit 5";

$query = mysql_query($sql) or die("".mysql_error());

while( $show = mysql_fetch_object( $query ) ) {

echo stripslashes($show->naam);

echo stripslashes($show->link);
}
echo "test";

?>


and it shows an empty space (it's not that the php is wrong, not the entire page is blank, just the place where the $show should come, remains empty)

hope you can help me
Thanks in advance, Jord

enigma1

9:51 am on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Get rid of the short tags and check the db connection, I see a second connection to the database inside the content.

eval('?>' . $show->content. '<?php ');

Where content is this:
<?php
.....

Matthew1980

9:54 am on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jord,

<?php<--Will be better accepted when you tell the parser what it is!
$con = mysql_connect("localhost","username","password") or die('Could not connect: '.mysql_error());


Try that first, and at the top of every php file your using turn error reporting on so that you can see if ANYTHING goes wrong!

error_reporting(E_ALL);

Cheers,
MRb

Jord

10:36 am on Oct 21, 2010 (gmt 0)

10+ Year Member



@enigma

connections both work, when i make it an echo, and change the content to 'regular' text it works perfectly

will try the shorttag thing


p.s.
I also tried an <?php echo "test";?> which didn't work either

Matthew1980

11:55 am on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jord,

Post what you are using now for this little script you have, then we can better analyse it and ergo try to fix it!

Cheers,
MRb

Jord

12:04 pm on Oct 21, 2010 (gmt 0)

10+ Year Member



It's pretty simple

on a page I have this:

<?php
//con has been made already
$sql = "SELECT * FROM sidebar WHERE id = '1' ORDER BY id DESC limit 1";
$query = mysql_query($sql) or die("".mysql_error());
while( $show = mysql_fetch_object( $query ) ) {


eval("?>" . $show->content. "<?php");
} ?>


This is to show the content in the sidebar, however in my sidebar I want to show the last 5 articles

so "content" in sidebar (see query) is this:

<?php 
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);


$sql = "SELECT * FROM artikelen limit 5";

$query = mysql_query($sql) or die("".mysql_error());

while( $show = mysql_fetch_object( $query ) ) {

echo stripslashes($show->naam);

echo stripslashes($show->link);
}
echo "test";

?>


This way it should echo: naam (name of the article) and link (of the article)

However the sidebar remains empty, so it fails to get the php code (for getting the last 5 articles) out the database. Eval should fix that, but it doesn't :(

I hope my english is clear enough for you to understand


p.s.
I used error_reporting, doesn't show anythng

enigma1

2:18 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is still incorrect, you haven't setup the php tag for the eval. Make sure you copy the exact line including the space from what I posted earlier in the eval line. That wasn't a typo.

rocknbil

3:01 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe I'm missing something, why do you need an eval? By the looks of your other queries to the object,

echo stripslashes($show->link);

Even that seems a little hinky, any class that has a method named "show" seems like it would just output and maybe you don't need an echo. (?)

$show->link;

You should just be able to do

<?php
//con has been made already
$sql = "SELECT * FROM sidebar WHERE id = '1' ORDER BY id DESC limit 1";
$query = mysql_query($sql) or die("".mysql_error());
while( $show = mysql_fetch_object( $query ) ) {

$show->content;

} ?>

The answer would lie in any documentation for the class you're using.