1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Fetching php code from database and then executing it.

Discussion in 'PHP' started by Josh-H, May 31, 2008.

  1. #1
    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?
     
    Josh-H, May 31, 2008 IP
  2. hkarakis

    hkarakis Active Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #2
    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 '...'
     
    hkarakis, May 31, 2008 IP
  3. Josh-H

    Josh-H Active Member

    Messages:
    406
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    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-H, May 31, 2008 IP
  4. swordbeta

    swordbeta Banned

    Messages:
    225
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    swordbeta, May 31, 2008 IP
  5. Josh-H

    Josh-H Active Member

    Messages:
    406
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    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..
     
    Josh-H, May 31, 2008 IP
  6. swordbeta

    swordbeta Banned

    Messages:
    225
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 :p
     
    swordbeta, May 31, 2008 IP
  7. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #7
    echo '$music'; // result is $music
    echo "$music"; // result is the value in the variable $music
     
    mehmetm, May 31, 2008 IP
  8. Josh-H

    Josh-H Active Member

    Messages:
    406
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #8
    Thank you, very much :)
     
    Josh-H, May 31, 2008 IP
  9. Josh-H

    Josh-H Active Member

    Messages:
    406
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #9
    Wow, just learnt something.

    The above code still isn't working, the one with preg replace, still getting same result..
     
    Josh-H, May 31, 2008 IP
  10. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #10
    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>
     
    mehmetm, May 31, 2008 IP
  11. Josh-H

    Josh-H Active Member

    Messages:
    406
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #11
    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!
     
    Josh-H, Jun 1, 2008 IP