Variables in a sql row?
Moderator: General Moderators
Variables in a sql row?
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!
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!
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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;
?>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.
trywhat you get from mysql is treated more like the second echo-literal.
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';
?>you will only get the last entry, $stuff is overwritten in each loop-iteration.while($content = mysql_fetch_object($query_result))
{
$stuff = $content->row;
}
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.
tryfor example
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";
}Oh... I have tried this:
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!
Code: Select all
<?php
while($content = mysql_fetch_object($query_result))
{
echo str_replace("\$var", "$var", $content->$row);
}
?>Thanks!
Please remove the " with the second param.The One wrote:Code: Select all
<?php while($content = mysql_fetch_object($query_result)) { echo str_replace("\$var", "$var", $content->$row); } ?>
str_replace("\$var",$var, $content->$row);
It such a waist of your keyboard...