importing PHP code from a MySQL database
Moderator: General Moderators
importing PHP code from a MySQL database
Unexpected problem: When I have some PHP code within an HTML block that I'm pulling from a MySQL table, the code is not recognized/run. It gets ignored as just any old bit of HTML nonsense between angle brackets.
I'm not sure why that is, except to guess that it has something to do with the retrieved field (with the HTML/PHP) being incorporated into my page at some point when it's too late for the PHP to get interpreted.
Anyone know of a way around it? A way to include usable PHP from a MySQL database for pages generated on the fly?
Thanks!
I'm not sure why that is, except to guess that it has something to do with the retrieved field (with the HTML/PHP) being incorporated into my page at some point when it's too late for the PHP to get interpreted.
Anyone know of a way around it? A way to include usable PHP from a MySQL database for pages generated on the fly?
Thanks!
how do you insert the php-portion?
assuming you just print it (
) take a look at http://www.php.net/manual/en/function.eval.php
ahh, and btw: read this post ,too
It's not straight related to your problem but has the same background
assuming you just print it (
ahh, and btw: read this post ,too
It's not straight related to your problem but has the same background
Hmmmmm, it seems eval() is a tiny bit tricky to use and my initial five or six experiments have ended in dismal failure -- even though I copied some examples from php.net directly into my MySQL table as a test. I'll keep experimenting, but in the meantime:
Anybody know if executing PHP via "eval" is a problem when the field is defined as a "blob" as opposed to varchar?
Anybody know if executing PHP via "eval" is a problem when the field is defined as a "blob" as opposed to varchar?
Maybe I should elaborate slightly, because my errors are unlike the errors described in the manual.
The manual says that echoing a variable without putting it through eval will result in the literal variable name being displayed instead of the value of that variable.
That's not what is happening to me.
In my case, nothing is displayed. Everything between "<?php" and "?>" is completely ignored by the browser.
As noted earlier, I've pasted in the "how to do it" examples from php.net's man pages, so I'm reasonably confident the code itself is OK. As for the text being imported from the database, it is basic HTML text stored in a "blob" field. Right in the middle of the HTML, I've inserted "<?php" and then put in my code and then closed the php with "?>" -- all very standard, all works fine if I call it as a separate php script NOT from the database.
So if this additional info triggers any additional insights from anyone, I'm all ears!
The manual says that echoing a variable without putting it through eval will result in the literal variable name being displayed instead of the value of that variable.
That's not what is happening to me.
In my case, nothing is displayed. Everything between "<?php" and "?>" is completely ignored by the browser.
As noted earlier, I've pasted in the "how to do it" examples from php.net's man pages, so I'm reasonably confident the code itself is OK. As for the text being imported from the database, it is basic HTML text stored in a "blob" field. Right in the middle of the HTML, I've inserted "<?php" and then put in my code and then closed the php with "?>" -- all very standard, all works fine if I call it as a separate php script NOT from the database.
So if this additional info triggers any additional insights from anyone, I'm all ears!
of course it is (at least most of the time). It's not the browser that does something with <?php ... ?> it's the server-side parser.Everything between "<?php" and "?>" is completely ignored by the browser.
If you can see the <?php .. ?> of the page in the browser-source-view then either something is wrong with your server-settings or you're doing something wrong whit eval() (unlikely since you copied examples).
Just a stupid question (only to be sure), what does
Code: Select all
<?php phpinfo(); ?>p.s.: oops sorry, I just read the part with "...all works fine if...", so forget about phpinfo()
Yes, if I "view source" of HTML I can see the literal PHP code there that I entered. So possibly there's something wrong with the server settings, although I'm using pair.com and as far as I know they're perfectly well configured.
I'll ask 'em about it though.
One other note about the code itself, which, yes, I'm copying from the manual and which should be good: I don't see how the server is going to recognize that an eval function has been called if it doesn't recognize that anything else PHP-related is happening there, if you see what I mean. So here's a general question:
How is it that the server can't understand the rest of the PHP if it can understand the PHP function "eval" all of a sudden? In other words, why should "eval" ever be necessary? What is it that prevents the non-"eval"-ified code from being understood?
I'll ask 'em about it though.
One other note about the code itself, which, yes, I'm copying from the manual and which should be good: I don't see how the server is going to recognize that an eval function has been called if it doesn't recognize that anything else PHP-related is happening there, if you see what I mean. So here's a general question:
How is it that the server can't understand the rest of the PHP if it can understand the PHP function "eval" all of a sudden? In other words, why should "eval" ever be necessary? What is it that prevents the non-"eval"-ified code from being understood?
In case anybody would care to examine the page in question, I've turned it into a "Problem Example Page":
http://www.wossafockenpoint.com/index.p ... le=contrib
You can view the source code and see exactly what I'm talking about.
http://www.wossafockenpoint.com/index.p ... le=contrib
You can view the source code and see exactly what I'm talking about.
First of all, thanks for sticking with this topic, your persistence is greatly appreciated.
The server is definitely set up to handle php, I use php all over the site with no problem. (And yes, I did try the phpinfo function to see what would happen, and it returned an extensive page of, well, PHP info -- check it at http://www.bonkworld.org/phpinfo.php if you think it might contain any useful clues). It's just this one problem with getting PHP to run when it is contained in a file imported from a MySQL table.
Weird eh? At least it's not just me who thinks so!
The server is definitely set up to handle php, I use php all over the site with no problem. (And yes, I did try the phpinfo function to see what would happen, and it returned an extensive page of, well, PHP info -- check it at http://www.bonkworld.org/phpinfo.php if you think it might contain any useful clues). It's just this one problem with getting PHP to run when it is contained in a file imported from a MySQL table.
Weird eh? At least it's not just me who thinks so!
Because, when you retrieve the data from MySQL, it comes back as a string. Whether that string contains PHP or no is not known. PHP doesn't treat it any differently. If you make PHP code into a string, it's just that, a string of characters. PHP doesn't invade a variables value, and start automatically parsing PHP it see's, not only would that cause more problems than it's worth, but it would be the biggest security hole this side of Redmond.In other words, why should "eval" ever be necessary? What is it that prevents the non-"eval"-ified code from being understood?
So you have to use Eval if you want to evaluate the PHP in a string as actual PHP.
Now, my assumption by what I see in viewing your source is that you are not using eval correctly. However, I would need to see your PHP code, so please post a .phps or post the code here for us to pick apart.
OK, the lights are slowly beginning to turn on in my head. Thanks to everyone who responded. I've got it figured out now.
I was doing something embarrassingly dumb, it turns out -- I was including the eval function within the code that was being imported, rather than within the code that was doing the importing. Once the big "DUH!" hit me I was on my way to solving the problem.
Thanks again...
I was doing something embarrassingly dumb, it turns out -- I was including the eval function within the code that was being imported, rather than within the code that was doing the importing. Once the big "DUH!" hit me I was on my way to solving the problem.
Thanks again...