SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Variable Help

    When I query my database I have a field which outputs the string $alang28.

    It outputs in my browser as $alang28. I also have another file which I have included on the page which defines $alang = "username:";

    How can I make it not output the string but output whats in the variable?
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Posts
    26
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    have you used short PHP opening and closing tag? please check that and check your PHP configuration file, does it support short tags

  3. #3
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it does support short tags
    Animated Chatrooms - www.121chatrooms.net

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Posts
    26
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    how you are browsing your PHP page or can you paste your code here

  5. #5
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so I have this code on my page:

    $alang2 = "Hello there";
    PHP outputs this from a field in the MySQL database. Testing $alang2. It outputs on the webpage as:

    Testing $alang2, where it should output: Testing Hello there

    How can I make it output the variable, it outputs fine if it dosnt come from the database.
    Animated Chatrooms - www.121chatrooms.net

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Paste the source code here, it is a bit hard to debug without the source. Sounds like you might not be using the database results correctly. try a var_dump or a print_r on the row results (object or array) from the database and make sure you are calling the correct variable.

  7. #7
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $alang28 
    "Hello there";

    $query5 mysql_query("SELECT * FROM onlineusers WHERE username ='$myuser' ")
    or die(
    "Could not insert data because ".mysql_error());
    $qry5 mysql_fetch_array$query5 );

    echo 
    "Testing $qry5[message]";
    echo 
    "Testing without database $alang28";  // Works here
    ?>
    qry5[message] outputs $alang28
    Animated Chatrooms - www.121chatrooms.net

  8. #8
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks like it should work after the fetch array, try print_r($qry5); and post the output.

  9. #9
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what it outputted:

    Testing testing $alang28
    again without database Hello thereArray ( [0] => 1 [id] => 1 [1] => chatpoint [username] => chatpoint [2] => blah [date] => blah [3] => testing $alang28 [command] => testing $alang28 ) daaa

    I changed my code to this:

    PHP Code:
    <?php
    include "includes/config.php";
    $alang28 "Hello there";

    $query5 mysql_query("SELECT * FROM logs_staff WHERE id ='1' ")
    or die(
    "Could not insert data because ".mysql_error());
    $qry5 mysql_fetch_array$query5 );

    echo 
    "Testing $qry5[command]<br />";
    echo 
    "again without database $alang28";  // Works here
    print_r($qry5);
    ?> daaa
    Animated Chatrooms - www.121chatrooms.net

  10. #10
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [command] => testing $alang28

    Thats whats stored in the database, it is correctly working. Update the database if you want it to display something else. If you are trying to process the variable inside the output then you shouldnt be doing it this way but you can do it like this (bewarned this isnt great security)

    eval("print 'Testing '.$qry5[command].'<br />';");

    or something to that affect. I would however make that $alang28 come from the database, it would be a much better design.

  11. #11
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm I gave that code a go and I got internal server error
    Animated Chatrooms - www.121chatrooms.net

  12. #12
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone else?
    Animated Chatrooms - www.121chatrooms.net

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Posts
    26
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you can replace the variable with value using str_replace()

  14. #14
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand?
    Animated Chatrooms - www.121chatrooms.net

  15. #15
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    513
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Single quote your keys and use brackets if echoing an array-key pair.
    PHP Code:
    echo "Testing {$qry5['message']}"

  16. #16
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    513
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I didn't notice you were storing a variable in the database. Not a good idea. In any case, you might try this.
    PHP Code:
    $var preg_replace("/[^a-zA-Z0-9_]+/"""$qry5['message']);
    $qry5['message'] = $$var

    echo 
    "Testing {$qry5['message']}"
    EDIT: Then again looking closer the variable is in a string so that's not going to work.

  17. #17
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    513
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    I suppose this would work.
    PHP Code:
    <?php
    $words 
    explode(" ",$qry5['message']);

    $sentence = array();
    foreach(
    $words as $str){
        if(
    strlen($str)>&& $str[0] == "$"){
            
    $part preg_replace("/[^a-zA-Z0-9_]+/"""$str);
            
    $part = $$part;
        }else{
            
    $part $str;
        }
        
    $sentence[] = $part;
    }
    $sentence implode(" ",$sentence);
    $qry5['message'] = $sentence

    echo 
    "Testing {$qry5['message']}";  
    ?>

  18. #18
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome thanks!
    Animated Chatrooms - www.121chatrooms.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •