Hello. I'm fetching the code <div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div> Code (markup): from a database (mysql). But when I echo it, I want it to echo the variables instead of $music or $tv How would I go about this?
echo "<div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div>"; that must work. Note: You should use "..." with echo instead of '...'
No the problem is that I'm fetching that string from a database: $result = mysql_query("SELECT * FROM templates") or die(mysql_error()); $row = mysql_fetch_array( $result ); $template = $row['template']; Code (markup): its fetching the string I posted earlier.
Josh, could you be more clear?What string?do you mean a row? $row = mysql_fetch_array(mysql_query("SELECT * FROM templates")); $music = $row['music']; $tv = $row['tv']; echo '<div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div>'; Code (markup): ?? @hkarakis: echo ''; and echo ""; don't have any difference, except that people believe that '' is faster
Sorry for the confusion. Heres my full code, I've just started: <?php $music = $_POST['music']; $tv = $_POST['tv']; if (isset($_POST['submit'])) { $host = "localhost"; $user = "root"; $pass = ""; $db = "dunno"; @mysql_connect($host,$user,$pass) or die("Could not connect to MySQL<br />".mysql_error()); @mysql_select_db($db) or die("Could not connect to MySQL database $db"); $result = mysql_query("SELECT * FROM templates") or die(mysql_error()); $row = mysql_fetch_array( $result ); $template = $row['template']; echo '<textarea name="textarea" id="textarea" cols="45" rows="5">'.$template.'</textarea>'; } else { echo' <form method="post" action="'.$PHP_SELF.'" > <label>music <textarea name="music" id="music" cols="45" rows="5"></textarea> </label> <br> <label>tv <textarea name="tv" id="tv" cols="45" rows="5"></textarea> </label> <input type="submit" name="submit" id="submit" value="submit"> </form>'; } ?> Code (markup): The mysql query is fetching the row <div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div> Code (markup): When I then echo $template it just echoes <div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div> Code (markup): instead of filling in the variables from the form..
Ow, ok, I get it, I've this too when storing PHP in MySQL, dunno exactly why but it's not possible. You can use preg_replace to replace it with the real string. <?php $music = $_POST['music']; $tv = $_POST['tv']; if (isset($_POST['submit'])) { $host = "localhost"; $user = "root"; $pass = ""; $db = "dunno"; @mysql_connect($host,$user,$pass) or die("Could not connect to MySQL<br />".mysql_error()); @mysql_select_db($db) or die("Could not connect to MySQL database $db"); $result = mysql_query("SELECT * FROM templates") or die(mysql_error()); $row = mysql_fetch_array( $result ); $template = $row['template']; $template = preg_replace("'\.\$music\.'",$music,$template); $template = preg_replace("'\.\$tv\.'",$tv,$template); echo '<textarea name="textarea" id="textarea" cols="45" rows="5">'.$template.'</textarea>'; } else { echo' <form method="post" action="'.$PHP_SELF.'" > <label>music <textarea name="music" id="music" cols="45" rows="5"></textarea> </label> <br> <label>tv <textarea name="tv" id="tv" cols="45" rows="5"></textarea> </label> <input type="submit" name="submit" id="submit" value="submit"> </form>'; } ?> PHP: I might have escaped too much, but it's your task to figure that out
Wow, just learnt something. The above code still isn't working, the one with preg replace, still getting same result..
firstly, try to insert this string to database and then execute it <div id="music">Music:<br>$music</div><div id="tv">TV:<br>$tv</div> instead of <div id="music">Music:<br>'.$music.'</div><div id="tv">TV:<br>'.$tv.'</div>
I did try that. I'm deciding to go a totally different away about this problem now thanks to preg_replace I will simple have some random jibberish such as 04music34 and replace that with the variable once fetched from the database Thanks!