Kirk Babb <mailto:[EMAIL PROTECTED]>
    on Monday, October 06, 2003 4:57 PM said:

> If I have a class called Registration, should I code in the
> mysql_connect inside each function inside the class that needs it, or
> should I make a function called dbConnect?

What you should do is define a db class and then extend that class with
the registration class.

(This is basically how I've been doing it. I'm no expert so it's very
possible this could be done in a more efficient way. Use at your own
risk. ;) )

(p.s. This is just pseudo-code)

Class Database
{
        var $Result;

        function Database()
        {
                // create usable db connection
        }

        function Query($sql)
        {
                // send a sql statement to the db
                // store the results in $this->Result;
        }

        function GetResults()
        {
                // turn $this->Result into useable
                // array and return it
        }
}

Class Registration extends Database
{
        function AddRegistrant($name, $age)
        {
                $sql = "INSERT INTO registrants\n"
                        ."      ( name\n"
                        ."      , age )\n"
                        ."VALUES\n"
                        .       ( $name\n"
                        .       , $age )";
                $this->query($sql);
        }

        function GetRegistrants()
        {
                $sql = "SELECT name, age FROM registrants";
                $this->query($sql);
                $rsRegistrants = $this->GetResults();

                return $rsRegistrants;
        }
}


HTH,
Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to