Jump to content

Archived

This topic is now archived and is closed to further replies.

Melon Fresh

Using Eval?

Recommended Posts

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.
------------------------------------------------------

[code=php:0]
$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");
}
[/code]

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

Any thoughts?

Share this post


Link to post
Share on other sites
Post the actual code that doesn't work and tell use [i]what[/i] the errror your getting is.

Share this post


Link to post
Share on other sites
why would you use parenthesis when echoing something?

Share this post


Link to post
Share on other sites
try [code]<?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";
?>[/code]

Share this post


Link to post
Share on other sites
Sorry for putting it in the wrong place, and here is the code that gives me the error
[code]
<?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]
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
[code]
<?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\";");
}
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
[code=php:0]
$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";
}
[/code]

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

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

Share this post


Link to post
Share on other sites
try[code]<?php
$userid =1;
$usernameid = 5;
$a ='<a href="index.php?logout=$userid" target="_parent" >Logout $usernameid </a>';
eval('$a = "'.addslashes($a).'";');
echo $a;
?>[/code]

Share this post


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

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.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.