Page 1 of 1

PHP Parsing and text

Posted: Mon Jul 07, 2003 7:30 pm
by rilus
I'm not sure this question has been asked before, but since I wasn't even sure how to look for it, I couldn't find anything about it on the forums.

First of all, I am a 2-day PHP newbie.

Now, I am trying to retrieve some info from a database, then I want to include whatever I retrieved into a php file.

Over simplified example:

name field contains:

Code: Select all

<?php
     print "Here's the info";
?>
Here's the php file which will be retrieving the info

Code: Select all

<?php
     $info=mysql_query("select name from table");
     $name=mysql_fetch_row($info);

     //the next part is what confuses me...
     //if I use echo or print all it does is output:
     //<?php
     //     print "Here's the info";
     //?>
?>
I need to know what to do so it will actually execute the code in the database field and not just print it.

It's kind of hard for me articulate this question, but I hope someone can give me some pointers on this.

Posted: Mon Jul 07, 2003 8:18 pm
by m3rajk
first off you don't need to re-open php. that might be part of the issue. the other part is you're giving a string without a variable

Code: Select all

<?php 
     $info=mysql_query("select name from table"); 
     $name=mysql_fetch_row($info); 

     //the next part is what confuses me... 
     //if I use echo or print all it does is output: 
     echo "Here's the $info"; 
?>
this still wont do what you want. however, it will now print whatever the return of $info was.

http://us3.php.net/manual/en/control-st ... oreach.php

use that link. it will show you a function called "foreach"

this function strikes me as the way to accomplish what i think you want to accomplish.

sorry if you expected something functional in response, but i know that personally it wouldn't teach me much, and i want you to learn why your problem is occuring and how to fix it so you wont encounter it again

Posted: Mon Jul 07, 2003 8:56 pm
by rilus
well...
what I want is for php to not simply ouput the text straight off from the database, as is... I want it to include it or somehow execute it as part of the php file that's retrieving the data...

Posted: Mon Jul 07, 2003 9:02 pm
by macewan
while ($row = mysql_fetch_array($sql_result_info)){

Posted: Mon Jul 07, 2003 10:22 pm
by m3rajk
http://us2.php.net/manual/en/control-st ... .while.php

what else do you want? just to show as you retreive it? or is this a debug as you put together what you're really doing?

if the latter, then use the foreach i have suggeasted, then you can modify that at each debug step

Posted: Mon Jul 07, 2003 10:38 pm
by McGruff
In your example $name is an array with a key=>value pair: 0=>'whatever is in the name col'.

If that is code, you would use eval() to execute it.

However, rather than storing php in a database, it's more common to include code from a script file, ie:

include('path/to/script.php'');

Posted: Tue Jul 08, 2003 4:25 am
by rilus
Thank you so much, McGruff! That answered my question... I needed the eval() function... And yea, I do use includes, but I need this code to be more dynamic.

Thanks again