Web Analytics Made Easy -
StatCounter Help with OOP Code - CodingForum

Announcement

Collapse
No announcement yet.

Help with OOP Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with OOP Code

    This is really my first major project with OOP, I'm building a blog script. So my problem is an error that I am getting here is the error
    Code:
    Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\server\htdocs\blog\blog.php on line 68
    and my code is here
    PHP Code:
    <?php

    class blog {
        
        var 
    $host;
        var 
    $password;
        var 
    $username;
        var 
    $database;
        
        var 
    $author;
        var 
    $email;
        var 
    $mood;
        var 
    $date;
        var 
    $entry;
        
        function 
    connect($host,$password,$username,$database)
        {
            
            
    $this->host $host;
            
    $this->password $password;
            
    $this->username $username;
            
    $this->databse $database;
            
            
    $link mysql_connect($this->host,$this->username,$this->password) or die("Could not connect to MySQL database server");
            
    mysql_select_db($this->database,$link) or die("Could not select database");
            
        }
        
        function 
    insert($author,$email,$mood,$entry)
        {
            
    $this->author $author;
            
    $this->email $email;
            
    $this->mood $mood;
            
    $this->date date("l,F j,Y @ G:i A");
            
    $this->entry $entry;
            
            
    $result "INSERT INTO `blog`(`id`,`author`,`email`,`mood`,`date`,`entry`) VALUES('NULL','{$this->author}','{$this->email}','{$this->mood}','{$this->date}','{$this->entry}')";
            
    mysql_query($result);    
        }
        
        function 
    select()
        {
        
            
    $result "SELECT id,author,email,mood,date,entry FROM blog ORDER BY id ASC";
            
    $query mysql_query($result,$link) or die(mysql_error());
            if(
    $query){
                if(
    mysql_num_rows($query) == 0){
                    echo 
    "No information in database!";
                    }
                else{
                    while(
    $row mysql_fetch_assoc($query)){
                        
    $this->author $row['author'];
                        
    $this->email $row['email'];
                        
    $this->mood $row['mood'];
                        
    $this->date $row['date'];
                        
    $this->entry $row['entry'];
                    }
                }
            }
        }

    $exe = new blog/* Line 68 */
    $exe->connect('localhost','root','rootpass','test');
    $exe->insert('Stevie Peele','[email protected]','Happy','Just a test');
    ?>
    Like I said, I'm relatively new to OOP, so go easy Thanks a lot
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    You left out a parenthesis, and forgot to close '}' your class defintion. here's the fixed code:
    PHP Code:
    <?php

    class blog {
        
        var 
    $host;
        var 
    $password;
        var 
    $username;
        var 
    $database;
        
        var 
    $author;
        var 
    $email;
        var 
    $mood;
        var 
    $date;
        var 
    $entry;
        
        function 
    connect($host,$password,$username,$database)
        {
            
            
    $this->host $host;
            
    $this->password $password;
            
    $this->username $username;
            
    $this->databse $database;
            
            
    $link mysql_connect($this->host,$this->username,$this->password) or die("Could not connect to MySQL database server");
            
    mysql_select_db($this->database,$link) or die("Could not select database");
            
        }
        
        function 
    insert($author,$email,$mood,$entry)
        {
            
    $this->author $author;
            
    $this->email $email;
            
    $this->mood $mood;
            
    $this->date date("l,F j,Y @ G:i A");
            
    $this->entry $entry;
            
            
    $result "INSERT INTO  `blog`(`id`,`author`,`email`,`mood`,`date`,`entry`) VALUES('NULL','{$this->author}','{$this->email}','{$this->mood}','{$this->date}','{$this->entry}')";
            
    mysql_query($result);    
        }
        
        function 
    select()
        {
        
            
    $result "SELECT id,author,email,mood,date,entry FROM blog ORDER BY id ASC";
            
    $query mysql_query($result,$link) or die(mysql_error());
            if(
    $query){
                if(
    mysql_num_rows($query) == 0)) {
                    echo 
    "No information in database!";
                    }
                else{
                    while(
    $row mysql_fetch_assoc($query)){
                        
    $this->author $row['author'];
                        
    $this->email $row['email'];
                        
    $this->mood $row['mood'];
                        
    $this->date $row['date'];
                        
    $this->entry $row['entry'];
                    }
                }
            }
        }
    }
    $exe = new blog/* Line 68 */
    $exe->connect('localhost','root','rootpass','test');
    $exe->insert('Stevie Peele','[email protected]','Happy','Just a test');
    ?>

    this missed line was near the end:

    if(mysql_num_rows($query) == 0)) {

    and the last closing brace.


    HTH,
    -Celt

    Comment


    • #3
      You have a typo here:

      $this->databse = $database;


      $this->database = $database;
      Spookster
      CodingForum Supreme Overlord
      All Hail Spookster

      Comment


      • #4
        Celtboy's tip did not work and I think it was incorrect. Here is my modified code, with the typo fixed and some modifications. My error is the same as above.
        PHP Code:
        <?php
        class blog {
            
            var 
        $host;
            var 
        $password;
            var 
        $username;
            var 
        $database;
            
            var 
        $author;
            var 
        $email;
            var 
        $mood;
            var 
        $date;
            var 
        $entry;
            
            function 
        connect($host,$user,$pass,$database)
            {
                
        $this->host $host;
                
        $this->user $user;
                
        $this->pass $pass;
                
        $this->database $database;
                    
                
        $link mysql_connect($this->host,$this->user,$this->pass) or die("Could not connect to MySQL database server");
                
        mysql_select_db($this->database,$link) or die("Could not select database");
                
            }
            
            function 
        insert($author,$email,$mood,$entry)
            {
                
        $this->author $author;
                
        $this->email $email;
                
        $this->mood $mood;
                
        $this->date date("l,F j,Y @ G:i A");
                
        $this->entry $entry;
                
                
        $result "INSERT INTO `blog`(`id`,`author`,`email`,`mood`,`date`,`entry`) VALUES('NULL','{$this->author}','{$this->email}','{$this->mood}','{$this->date}','{$this->entry}')";
                
        mysql_query($result);    
            }
            
            function 
        select()
            {
                
        connect();
                
        $result "SELECT id,author,email,mood,date,entry FROM blog ORDER BY id ASC";
                
        $query mysql_query($result,$link) or die(mysql_error());
                if(
        $query){
                    if(
        mysql_num_rows($query) == "0"){
                        echo 
        "No information in database!";
                        }
                    else{
                        while(
        $row mysql_fetch_assoc($query)){
                            
        $this->author $row['author'];
                            
        $this->email $row['email'];
                            
        $this->mood $row['mood'];
                            
        $this->date $row['date'];
                            
        $this->entry $row['entry'];
                        }
                    }
                }
            }

        $exe = new blog/* Error Here - 
        Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in c:\server\htdocs\blog\blog.php on line 67
         */
        $exe->connect("localhost","root","rootpass","test");
        $exe->insert('Stevie Peele','[email protected]','Happy','Just a test');
        ?>
        Thanks to all. . .
        Stevie Peele
        Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
        #dev - any programming,etc. question
        #design - design discussion and critque
        #central - general chat
        Come join us!

        Comment


        • #5
          3 things:

          1- I dunno what i was thinking about that missing parenth.

          2- It still looks like you're missing a closing curley brace } at the end of your class definition.

          3- try adding a contructor, even if blank:

          PHP Code:
          function blog() {
          return;

          -Celt

          Comment


          • #6
            celtboy answered the parse error question, but you have another error in your select() method. This

            PHP Code:
            connect(); 
            won't work if you don't have a global connect() function. And if you want to call Blog::connect, you have to use $this->connect(), and pass the connection parameters anew. A better approach would be to check if a connection has already established and only then calling the connect() method.
            De gustibus non est disputandum.

            Comment


            • #7
              Thanks for the help. I'll have to test the code when I get home from school.
              Stevie Peele
              Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
              #dev - any programming,etc. question
              #design - design discussion and critque
              #central - general chat
              Come join us!

              Comment


              • #8
                Works . . .

                Well, it works great. Don't be surprised if im back again for more help
                Stevie Peele
                Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                #dev - any programming,etc. question
                #design - design discussion and critque
                #central - general chat
                Come join us!

                Comment


                • #9
                  I'll be there to keep you on track, Stevie. :-D

                  Comment


                  • #10
                    As promised, I am back. Here is my problem. I am getting an error, for some reason. My error is this.
                    Fatal error: Cannot redeclare class blog in c:\server\htdocs\blog\blog.php on line 9
                    and for the sake of space, blog.php is located at http://www.net-riches.com/blogcode.phps

                    Could anyone know why this error is coming up?

                    Thanks
                    Stevie Peele
                    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                    #dev - any programming,etc. question
                    #design - design discussion and critque
                    #central - general chat
                    Come join us!

                    Comment


                    • #11
                      on whatever page you're including the class definition file, try changing the

                      PHP Code:
                      ...
                      include(
                      "blog.php");
                      ... 
                      to
                      PHP Code:
                      ...
                      include_once(
                      "blog.php");
                      ... 

                      Comment


                      • #12
                        I was using require(). Would that produce the error? I used it on one file and it worked fine, and the next file, it produced that error.
                        Stevie Peele
                        Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                        #dev - any programming,etc. question
                        #design - design discussion and critque
                        #central - general chat
                        Come join us!

                        Comment


                        • #13
                          possibly. try using "require_once()";

                          Comment


                          • #14
                            include_once worked.

                            I know why now. I was using my class to use two different functions in two different locations on the same page. Thats why.

                            Works now.

                            - Thanks
                            Stevie Peele
                            Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
                            #dev - any programming,etc. question
                            #design - design discussion and critque
                            #central - general chat
                            Come join us!

                            Comment

                            Working...
                            X