Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Coding in variables?

    It won't let me do this correctly? Im am trying to put a code in variables. Is this possible? Havn't gotten it to work. Let me know what you think.

    What the following code is trying to accomplish is to connect to a database, get info whether or not something is set, and then depeding on whether or not it is set, display random events, where one event is trying to update something in a database.

    PHP Code:
    <?php
    mysql_connect
    ("localhost""database""pass") or die(mysql_error());
    echo 
    "Connected to MySQL<br />";
    $lala mysql_query("SELECT * FROM event");
    while(
    $row mysql_fetch_array($lala))
      {
      
    $txt $row['event'];
    if (
    $txt == "Y"){
    $display "no"
    }
    else {
     
    $display "yes";
      } 
    if (
    $display == "yes"){

    $quotes[] = "Random event 1
    $result = mysql_query("
    UPDATE event SET event='Y' WHERE event='N'") 
    or die("
    Could not update");  


    $result = mysql_query("
    SELECT FROM event WHERE event='Y'") 
    or die(mysql_error());"
    ;
    $quotes[] = "Random Event 2";
    $quotes[] = "Random Event 3";
    $quotes[] = "Random Event 5";
    $quotes[] = "Random Event 6";
    $quotes[] = "Random Event 7";

    srand ((double) microtime() * 1000000);
    $randomquote rand(0,count($quotes)-1);

    echo 
    "<p>" $quotes[$randomquote] . "</p>";
    }
    else { echo 
    "You've already got the random event!";
    }
    ?>
    I know there's something wrong this:

    Line 15 $quotes[] = "Random event 1
    Line 16 $result = mysql_query("UPDATE event SET event='Y' WHERE
    Line 17 event='N'")
    Line 18 or die("Could not update");


    But I don't know what to do. If it put it as an echo statement inside the variable it messes it up more. In any event, I am getting this error:
    Parse error: syntax error, unexpected T_STRING in /home/username/public_html/test/event.php on line 15
    Last edited by nobackseat88; 03-14-2008 at 02:43 AM. Reason: Misspelling

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    You didn't close the "" for the first quote. I don't know how you missed that. So I added "; to the end. The if($txt check is worthless because you could just put the code in if the text equals Y. SO I removed that too. Here you go:

    PHP Code:
    <?php
    mysql_connect
    ("localhost""database""pass") or die(mysql_error());
    echo 
    "Connected to MySQL<br />";
    $lala mysql_query("SELECT * FROM event");
    while(
    $row mysql_fetch_array($lala))
    {
        
    $txt $row['event'];
        if (
    $txt == "Y"){
            
    $quotes[] = "Random event 1
            $result = mysql_query("
    UPDATE event SET event='Y' WHERE event='N'") 
            or die("
    Could not update");  

            $result = mysql_query("
    SELECT FROM event WHERE event='Y'") 
            or die(mysql_error());"
    ;
            
    $quotes[] = "Random Event 2";
            
    $quotes[] = "Random Event 3";
            
    $quotes[] = "Random Event 5";
            
    $quotes[] = "Random Event 6";
            
    $quotes[] = "Random Event 7";

            
    srand ((double) microtime() * 1000000);
            
    $randomquote rand(0,count($quotes)-1);

            echo 
    "<p>" $quotes[$randomquote] . "</p>";
        } else {
            echo 
    "You've already got the random event!";
        }
    }
    ?>
    Last edited by flynch01; 03-14-2008 at 03:00 AM.

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Okay, thanks will try it. The ending quote " slipped right past me. The mysql queries are to update the database when that random event is pulled from the code and displayed.

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Still doesn't work!

    Parse error: syntax error, unexpected T_STRING in /home/username/public_html/test/event.php on line 10
    PHP Code:
    <?php
    mysql_connect
    ("localhost""database""pass") or die(mysql_error());
    echo 
    "Connected to MySQL<br />";
    $lala mysql_query("SELECT * FROM event");
    while(
    $row mysql_fetch_array($lala))
    {
        
    $txt $row['event'];
        if (
    $txt == "Y"){
            
    $quotes[] = "Random event 1 <<< ERROR!
            $result = mysql_query("
    UPDATE event SET event='Y' WHERE event='N'") 
            or die("
    Could not update");  

            $result = mysql_query("
    SELECT FROM event WHERE event='Y'") 
            or die(mysql_error());"
    ;
            
    $quotes[] = "Random Event 2";
            
    $quotes[] = "Random Event 3";
            
    $quotes[] = "Random Event 5";
            
    $quotes[] = "Random Event 6";
            
    $quotes[] = "Random Event 7";

            
    srand ((double) microtime() * 1000000);
            
    $randomquote rand(0,count($quotes)-1);

            echo 
    "<p>" $quotes[$randomquote] . "</p>";
        } else {
            echo 
    "You've already got the random event!";
        }
    }
    ?>
    Where the <<< ERROR text is, the words to the left can't be in the middle of the php coding, doesn't work, generates error. And when I try to put it in a echo statement in the variable, it messes it up, as I said in the first post. So I don't know how to do this.
    Last edited by nobackseat88; 03-14-2008 at 03:10 AM.

  • #5
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    CLOSE IT!

    $quotes[] = "Random event 1";

    Code:
    ";
    Code:
    ";
    Code:
    ";
    Code:
    ";

  • #6
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    I want to include this code in the quotes[] variable!!!

    $result = mysql_query("UPDATE event SET event='Y' WHERE event='N'")
    or die("Could not update");

    $result = mysql_query("SELECT * FROM event WHERE event='Y'")
    or die(mysql_error());";

  • #7
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Code in quotes won't execute, but you can try it if you like. My point remains the same though. Theres no end quote at the end after die:

    PHP Code:
    $quotes[] = "Random event 1 <<< ERROR!
            $result = mysql_query("
    UPDATE event SET event='Y' WHERE event='N'") 
            or die("
    Could not update")";//Added " before ; 
    And considering the code is inside a quote, you need to escape the '"' so it's actually:


    PHP Code:
    $quotes[] = "Random event 1 <<< ERROR!
            $result = mysql_query(\"UPDATE event SET event='Y' WHERE event='N'\") 
            or die(\"Could not update\")"
    ;//Added " before ; 
    All it will do is echo that code though, it wont return a result.

  • #8
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    so it's not possible for it to contact the database and such inside a random event variable?

  • #9
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Oh I just saw how you included both mysql in there so my quote was wrong, it's because you weren't escaping your "s. Anyway no you can't contact the database inside a variable. You can however, just add the code after the quotes. Like so:

    PHP Code:

            $quotes
    [] = "Random event 1"
            
    $quotes[] = "Random Event 2";
            
    $quotes[] = "Random Event 3";
            
    $quotes[] = "Random Event 5";
            
    $quotes[] = "Random Event 6";
            
    $quotes[] = "Random Event 7";
            
    mysql_query("UPDATE event SET event='Y' WHERE event='N'"
            or die(
    "Could not update");
            
    mysql_query("SELECT * FROM event WHERE event='Y'"
            or die(
    mysql_error()); 
    That will contact the database.

  • #10
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Okay, thanks anyway. I only wanted the code to excute in that random event only. I've seen it before tho.

  • #11
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    What if I put it in an if statement, like if the random event chosen was the mysql one, then excute the code!

    EDIT: Nevermind, it's in an array.

  • #12
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    I'm confused as to what you mean. But you said nevermind, if you want more help though im watching the thread.


  •  

    Posting Permissions

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