Melon Fresh Posted October 5, 2006 Share Posted October 5, 2006 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? Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/ Share on other sites More sharing options...
trq Posted October 5, 2006 Share Posted October 5, 2006 Post the actual code that doesn't work and tell use [i]what[/i] the errror your getting is. Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-104606 Share on other sites More sharing options...
marcus Posted October 5, 2006 Share Posted October 5, 2006 why would you use parenthesis when echoing something? Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-104614 Share on other sites More sharing options...
sasa Posted October 5, 2006 Share Posted October 5, 2006 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] Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-104624 Share on other sites More sharing options...
Melon Fresh Posted October 6, 2006 Author Share Posted October 6, 2006 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] Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-105115 Share on other sites More sharing options...
obsidian Posted October 6, 2006 Share Posted October 6, 2006 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. Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-105123 Share on other sites More sharing options...
Melon Fresh Posted October 6, 2006 Author Share Posted October 6, 2006 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]<?phpinclude ("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] Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-105274 Share on other sites More sharing options...
obsidian Posted October 9, 2006 Share Posted October 9, 2006 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. Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-106041 Share on other sites More sharing options...
Melon Fresh Posted October 30, 2006 Author Share Posted October 30, 2006 Sorry for the late reply, yes you are right.What I am trying to do is draw variables from the mysql database e.g. $useridif you use echo mysql shows $useridbut 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] Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-116884 Share on other sites More sharing options...
sasa Posted October 30, 2006 Share Posted October 30, 2006 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] Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-116909 Share on other sites More sharing options...
obsidian Posted October 30, 2006 Share Posted October 30, 2006 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. Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-116924 Share on other sites More sharing options...
Melon Fresh Posted October 30, 2006 Author Share Posted October 30, 2006 Thank you sasa & obsidian all it working now :) Link to comment https://forums.phpfreaks.com/topic/23115-using-eval/#findComment-116978 Share on other sites More sharing options...
Recommended Posts