echoing a session variable from content within a database???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    echoing a session variable from content within a database???

    Hello everyone. :)

    I knew it wouldn't be very long before I was back with another question.

    I just created a login box on my site and I am having an issue with trying to echo an error back to the user. The page content is in a database and I tried to put echo $_SESSION['error']; and all I get is "echo $_SESSION['error'];" instead of the error string.

    Can someone please advise of a better way to do this? I want to keep the page in the db but yet I still have to let the user know that there was a problem logging them in if need be.

    Thanks,

    Frank
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You need to use delimiters.
    <?php echo $_SESSION['error'];?>

    Let me know if this helps.

    Comment

    • fjm
      Contributor
      • May 2007
      • 348

      #3
      Originally posted by markusn00b
      You need to use delimiters.
      <?php echo $_SESSION['error'];?>

      Let me know if this helps.
      Hi and thanks for the help. I should have stated that in my post but I have tried it that way and still nothing. All it does is echo <?php echo $_SESSION['error'];?>

      Could it be because the page in the database is loading before... or something. I'm out of answers..

      I just tried that again and looked at the html source and can see:

      <?php echo $_SESSION['error'];?>

      Just figured I would add that. :) Thanks for the help.

      Comment

      • fjm
        Contributor
        • May 2007
        • 348

        #4
        Does anyone have any ideas?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          If the PHP code is being stored in a database, you will have to explicitly tell PHP to treat it as code, rather than just plain text.

          Try using the eval function.

          Comment

          • fjm
            Contributor
            • May 2007
            • 348

            #6
            Originally posted by Atli
            If the PHP code is being stored in a database, you will have to explicitly tell PHP to treat it as code, rather than just plain text.

            Try using the eval function.
            I was very excited for a minute but it will just echo the eval function also. I just copied a small bit of code using the eval() function.

            [PHP]<?php
            $name = 'Joe';
            $name2 = 'Jim';
            $a = 'My friends are $name and $name2';
            print $a . "<br>";
            eval("\$a = \"$a\";");
            print $a . "<br>";
            ?>[/PHP]

            That is exactly what shows in the html source when I view it.

            Should I be using the eval function somewhere else and not in my database content?

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Yes, you should be using the eval function on the data from your database.
              Like:
              [code=php]
              $result = mysql_query("SE LECT pageContent FROM someTable");
              $row = mysql_fetch_ass oc($result);
              eval($row['pageContent']);
              [/code]
              Where the "pageConten t" column contains the entire HTML content, including the PHP statements.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Atli
                Yes, you should be using the eval function on the data from your database.
                Like:
                [code=php]
                $result = mysql_query("SE LECT pageContent FROM someTable");
                $row = mysql_fetch_ass oc($result);
                eval($row['pageContent']);
                [/code]
                Where the "pageConten t" column contains the entire HTML content, including the PHP statements.
                Ah!
                So simple when you think about it.

                Comment

                • fjm
                  Contributor
                  • May 2007
                  • 348

                  #9
                  Well, today must not be my day... I am getting the following errors when I use eval() as suggested.

                  Array to string conversion ... on line 66
                  Parse error: syntax error, unexpected $end, .... eval()'d code on line 1
                  Here is how I am using it:
                  $content = $db->Row();
                  return eval($content);

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    What is the actual content of the $content variable?

                    I think the eval function assumes the content is PHP code, so it shouldn't start with <?php or end with ?>.

                    Try adding that to the string:
                    [code=php]
                    eval("?>". $content ."<?php");
                    [/code]

                    Comment

                    • fjm
                      Contributor
                      • May 2007
                      • 348

                      #11
                      Alti,

                      I no longer have the errors and actually have some good news. The html source shows:

                      Array<?php

                      I think we are getting closer. By the way, I completely cleared the db table and put in starting <?php tag and then removed it. Both ways did not work.

                      EDIT:
                      Forgot to answer your question. The only content in the table is pure html code. div tags, html and body tags

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        It shouldn't start with <?php
                        The eval function will consider the contents it is passed to be PHP code, so no tags are required.

                        If, however, the contents are not PHP code, but rather HTML, as it is with you. You must close the PHP tag before it can be used.

                        Consider this:
                        [code=php]
                        $content = "
                        <h1>Testing.. </h1>
                        <?php echo \"whott\"; ?>
                        <h2>Daddaradaaa !!</h2>
                        ";

                        eval("?>". $content ."<?php");
                        [/code]

                        Comment

                        • fjm
                          Contributor
                          • May 2007
                          • 348

                          #13
                          Atli, I totally cleared out the db table so there are no php tags or html tags in there now. (the table contents do not have any php tags what so ever anyhow; just thought I would throw that in)I an getting "unexpected $end" error with the code you last posted. I looked it over and I don't see any errors. ::confused::

                          Comment

                          • fjm
                            Contributor
                            • May 2007
                            • 348

                            #14
                            Atli,

                            this works:
                            $content = "dfsfg";
                            return eval("?>". $content ."<?php");

                            I can see "dfsfg" being echoed to the screen. Your code (<?php echo \"whott\"; ?>) was creating the error because it is being parsed inside of another php script (different script). This script processes the database content.

                            When I got rid of your start and end php tags in your <h2> elements, it worked.

                            I commented the $content = "dfsfg"; line and opened up my original $content variable and am now getting "Array" in place of a single string I placed in the db table (Hello World)

                            Comment

                            • fjm
                              Contributor
                              • May 2007
                              • 348

                              #15
                              I am going to put this one on hold for a while. I need to focus my attention on my OOP user class because hey, what good is an error code being echoed from the database content if the user can't even connect. :)

                              Comment

                              Working...