Page 1 of 1

Variables in a sql row?

Posted: Sun Apr 20, 2003 4:18 am
by The One
First I have stored the value "xxx" in variable $x at the start of my php script, then in one of the sql rows, which has a string like "text text $x text text". Now in the php script after the $x variable, it connects to the database, grabs that row by mysql_fetch_object(), and I stored that into a variable $y. After that I print $y, but it prints exactly text text $x text text on the page instead of text text xxx text text, what is going on?? I have also tried echo but that just prints(or echos) text text $x text text! Please help me fix this up!
Thank you!

P.S. I know that you can put $variables in a mysql row, because there is alot of $variables in the vbulletin template(which are store as rows), and I did managed to put my own $variable in it as well... now I am writing my own script but it just don't wok!

Thank you!

Posted: Sun Apr 20, 2003 4:24 am
by The One
Welll plz MSN/AIM me if you see me online RIGHT NOW!

Posted: Sun Apr 20, 2003 5:19 am
by twigletmac
If you show us your actual code it'll make it a lot easier for us to help you debug.

Mac

Posted: Mon Apr 21, 2003 1:36 am
by The One

Code: Select all

<?php
$var = "<table><tr><td>some text</td></tr></table>";

require("./config.php"); //this contains the database information such as username, db name, etc, as assigned in variables

$db_connection = mysql_connect($servername, $dbusername, $dbpassword)
                 or die("Database unavailable");

    mysql_select_db($dbname, $db_connection)
                 or die("Database unavailable");
                 
    $sql_query = "SELECT * FROM table WHERE rowID = '1'";
                 
    $query_result = mysql_query($sql_query, $db_connection);
    while($content = mysql_fetch_object($query_result))
    {
      $stuff = $content->row;
    }

    mysql_close($db_connection);

print $stuff;
echo $stuff;
?>
the row I called out, which is $content->row has something like "blah blah blah $var blah blah blah", as I store it as $stuff and I print/echo out $stuff, it prints/echos exactly "blah blah blah $var blah blah blah" instead of "blah blah blah <table><tr><td>some text</td></tr></table> blah blah blah"

Posted: Mon Apr 21, 2003 7:05 am
by volka
php does not parse the string returned by msql as php-code. It only does for its own string literals (those you can see in your script).
you can use str_replace to accomplish that.
try

Code: Select all

<?php
$var = 'test';
echo "just a $var", "<br />\n";
echo 'just a $var';
?>
what you get from mysql is treated more like the second echo-literal.
while($content = mysql_fetch_object($query_result))
{
$stuff = $content->row;
}
you will only get the last entry, $stuff is overwritten in each loop-iteration.

Posted: Tue Apr 22, 2003 12:54 am
by The One
So how should I use str_replace? Note that I can't change the entry.

Posted: Tue Apr 22, 2003 6:54 am
by volka
You do not change the data in mysql if you manipulate the string in php.
To do that you would need another query sent to mysql.
try

Code: Select all

while($content = mysql_fetch_object($query_result))
{
	echo str_replace('e', 'E', $content->row), "<br />\n";
}
for example

Posted: Wed Apr 23, 2003 2:21 am
by The One
Oh... I have tried this:

Code: Select all

<?php
while($content = mysql_fetch_object($query_result))
    {
      echo str_replace("\$var", "$var", $content->$row);
    }
?>
And it worked... but, I want to do so that all variables would be parsed inside a Sql row... do you have any ideas?
Thanks!

Posted: Wed Apr 23, 2003 12:42 pm
by chris22
Why exactly are you wanting to store variables in a database?

If you're really desperate to put php code (variables) within a database, you might want to try the eval function. However, it is a pretty big security risk.

Posted: Wed Apr 23, 2003 11:45 pm
by The One
What does eval do? How do I use it?

Posted: Thu Apr 24, 2003 11:02 am
by chris22

Posted: Sat Apr 26, 2003 8:09 am
by The One
That was too confuse to look up tho... note that I am a beginner at programming...

Posted: Sat Apr 26, 2003 2:25 pm
by []InTeR[]
The One wrote:

Code: Select all

<?php
while($content = mysql_fetch_object($query_result))
    {
      echo str_replace("\$var", "$var", $content->$row);
    }
?>
Please remove the " with the second param.
str_replace("\$var",$var, $content->$row);

It such a waist of your keyboard...