Variables in a sql row?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Variables in a sql row?

Post 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!
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post by The One »

Welll plz MSN/AIM me if you see me online RIGHT NOW!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you show us your actual code it'll make it a lot easier for us to help you debug.

Mac
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post 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"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post by The One »

So how should I use str_replace? Note that I can't change the entry.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post 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!
chris22
Forum Newbie
Posts: 11
Joined: Tue Apr 22, 2003 9:45 pm

Post 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.
The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post by The One »

What does eval do? How do I use it?
chris22
Forum Newbie
Posts: 11
Joined: Tue Apr 22, 2003 9:45 pm

Post by chris22 »

The One
Forum Newbie
Posts: 8
Joined: Sun Apr 20, 2003 4:18 am

Post by The One »

That was too confuse to look up tho... note that I am a beginner at programming...
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

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