Jump to content


Photo

Using Eval?


  • This topic is locked This topic is locked
11 replies to this topic

#1 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 October 2006 - 09:41 PM

I am trying to learn how to use variables in mysql, and eval is what I need, the only problem is I don't get it, and when I have tried I always have error, so help please :)

Here is the code that I want eval to work for,at the moment it has echo, but I need eval.
------------------------------------------------------

$result = @mysql_query("SELECT * FROM `tech_templates` WHERE `tid` = 5");
 if (!$result) {
   exit('<p>Error performing query: ' .
       mysql_error() . '</p>');
 }

while($row = mysql_fetch_array($result)) 
    { 
        $code5 = $row['code']; 
		echo ("$code5");
		}

------------------------------------------------------

Any thoughts?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 October 2006 - 10:11 PM

Post the actual code that doesn't work and tell use what the errror your getting is.

#3 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 05 October 2006 - 10:21 PM

why would you use parenthesis when echoing something?

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 05 October 2006 - 10:31 PM

try
<?php
$a=8;
$b=7;
$c = '	echo "$a + $b = ";
		echo $c = $a + $b . "<br />\n";
		echo $c + $a . "<br />\n";
		for($i = 1; $i < 11; $i++) echo $i . "<br />\n";
	';
eval($c);
echo 'New value of $c is: ' . $c .'!!!!'; //$c = $a + $b . "\n";
?>


#5 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 04:29 PM

Sorry for putting it in the wrong place, and here is the code that gives me the error
<?php
$result = @mysql_query("SELECT * FROM `tech_templates` WHERE `tid` = 5");
 if (!$result) {
   exit('<p>Error performing query: ' .
       mysql_error() . '</p>');
 }

while($row = mysql_fetch_array($result)) 
    { 
        $code5 = $row['code']; 

eval("\$code5 = \"$code5\";");
}
?>
the error is [b]Parse error: syntax error, unexpected T_STRING in /******/******/public_html/index.php(30) : eval()'d code on line 4[/b]


#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 October 2006 - 04:41 PM

i'm not following why you need eval for what you're doing. all you are doing with the code you just posted is to re-asign the $code5 variable with a string interpreted version of that same $code5 variable. which line is line 30? it looks like you've got a syntax error somewhere else in the code that's causing the problem right now.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 10:05 PM

What I am trying to do is use variables in mysql, and eval i was told was the way to go, this is the whole code of the page
<?php
include ("global.php");
if (isset($_COOKIE["bgauth"])){
} else { header("Location: login.php");
exit; }

$result = @mysql_query("SELECT * FROM `tech_templates` WHERE `tid` = 5");
 if (!$result) {
   exit('<p>Error performing query: ' .
       mysql_error() . '</p>');
 }

while($row = mysql_fetch_array($result)) 
    { 
        $code5 = $row['code']; 

eval("\$code5 = \"$code5\";");
}
?>


#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 09 October 2006 - 12:44 AM

ah, i see what you're after. you're trying to store PHP variables within your mysql database and translate them upon retrieval? that can work, but those variables have to be set previously on the page to get it to do anything worthwhile. now that we can see what you're trying to do, run through with us what your expected outcome is and what you're actually getting.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 October 2006 - 07:00 PM

Sorry for the late reply, yes you are right.
What I am trying to do is draw variables from the mysql database e.g. $userid
if you use echo mysql shows $userid
but I know there is a way to show 1 (as the $userid = 1)
thats what I was hoping eval would do, I have tried it many times but no response;
here is my latest code and error
$result = @mysql_query("SELECT `code` FROM `tech_templates` WHERE `tid` = 13");
while($row = mysql_fetch_array($result)) 
    { 
	$new_page = eval($row['code']);

		echo "$new_page";
		}

The error is
Parse error: syntax error, unexpected '<' in /home/*****/public_html/****/access_control/index.php(23) : eval()'d code on line 1


Here is what is in the database
<a href="index.php?logout=$userid" target="_parent" >Logout $usernameid </a>


#10 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 30 October 2006 - 07:38 PM

try
<?php
$userid =1;
$usernameid = 5;
$a ='<a href="index.php?logout=$userid" target="_parent" >Logout $usernameid </a>';
eval('$a = "'.addslashes($a).'";');
echo $a;
?>


#11 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 October 2006 - 07:55 PM

My guess is that you'll want to use a slight variation on sasa's recommendation:
<?php
$userid = 1;
$usernameid = 5;
$a = '<a href=\"index.php?logout=$userid\" target=\"_parent\" >Logout $usernameid</a>';
eval("\$a = \"$a\";");
echo $a;
?>

Notice that you need to have the double quotes escaped in the database. I tested this, and it seems to do exactly what you're after.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#12 Melon Fresh

Melon Fresh
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 30 October 2006 - 09:55 PM

Thank you sasa & obsidian all it working now :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users