eval MySQL commands

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Steward

    #1

    eval MySQL commands

    I'm trying to create a generic object that will work for several
    different types of databases. Does anyone know why this won't work?

    // Connect to the database
    $cmd = $this->db_type . "_connect (\"" . $this->server . "\", \"" .
    $this->login . "\", \"" . $this->password . "\");";
    $this->id = eval($cmd);

    In this case, $this->db_type = "mysql".

    Thanks!
  • Guillaume Brocker

    #2
    Re: eval MySQL commands

    Steve Steward wrote:[color=blue]
    > I'm trying to create a generic object that will work for several
    > different types of databases. Does anyone know why this won't work?
    >
    > // Connect to the database
    > $cmd = $this->db_type . "_connect (\"" . $this->server . "\", \"" .
    > $this->login . "\", \"" . $this->password . "\");";
    > $this->id = eval($cmd);
    >
    > In this case, $this->db_type = "mysql".
    >
    > Thanks![/color]

    You could use object-oriented programming for that. Simply define an
    abstract DB connection class and implement this interface for each real
    DB connection desired.

    --
    Guillaume Brocker

    Comment

    • Chris Hope

      #3
      Re: eval MySQL commands

      Guillaume Brocker wrote:
      [color=blue]
      > Steve Steward wrote:[color=green]
      >> I'm trying to create a generic object that will work for several
      >> different types of databases. Does anyone know why this won't work?
      >>
      >> // Connect to the database
      >> $cmd = $this->db_type . "_connect (\"" . $this->server . "\", \"" .
      >> $this->login . "\", \"" . $this->password . "\");";
      >> $this->id = eval($cmd);
      >>
      >> In this case, $this->db_type = "mysql".
      >>[/color]
      > You could use object-oriented programming for that. Simply define an
      > abstract DB connection class and implement this interface for each real
      > DB connection desired.[/color]

      Or you could save yourself the effort and use either the PEAR or ADOdb
      database classes.

      Chris

      --
      Chris Hope
      The Electric Toolbox Ltd
      http://www.electrictoolbox.com/

      Comment

      • Steve Steward

        #4
        Re: eval MySQL commands

        Guillaume Brocker <guillaume.broc ker@ircad.u-strasbg.fr> wrote in message news:<40855336$ 0$21182$626a14c e@news.free.fr> ...
        [color=blue]
        > You could use object-oriented programming for that. Simply define an
        > abstract DB connection class and implement this interface for each real
        > DB connection desired.[/color]

        I am using OOP code. (Note the $this->id, etc.) The actual DB
        connection I'm not as worried about. It's all the other fuctions.
        (fetch rows, results, fields, etc)

        I had a project recently where the client switched servers when I was
        90% of the way through the project. Because of that, I had to go
        through all my DB code and change it from MySQL to MS SQL. I was
        hoping to create a fairly straight forward object where I could just
        change the "db_type" in the object definition to change databases.

        Because PHP using similar function names for most MySQL and MS SQL
        commands, I could set up a whole series of methods in my object to
        define what I want to do. So, I just say $db->getRow() and it handles
        the rest. I don't know about other database types other than MySQL and
        MS SQL. I don't have access to those databases to test it so I'm only
        worried about those two for now.

        In the object I just say:

        $cmd = $db_type . "_fetch_row($th is->results);";
        eval($cmd);

        But, this doesn't work for some reason.

        If I define objects for every type of database connection, then I
        haven't really gained anything have I? I will still have to deal with
        a whole bunch of objects instead of just one. I guess it might be a
        little simplier but it sounds like a lot of work for very little
        payoff. Am I missing something?

        Great suggestion, though. Good to see we're on the same track. :)

        Comment

        • Andy Hassall

          #5
          Re: eval MySQL commands

          On 20 Apr 2004 14:05:32 -0700, steve.steward@b hi1.com (Steve Steward) wrote:
          [color=blue]
          >Guillaume Brocker <guillaume.broc ker@ircad.u-strasbg.fr> wrote in message news:<40855336$ 0$21182$626a14c e@news.free.fr> ...
          >[color=green]
          >> You could use object-oriented programming for that. Simply define an
          >> abstract DB connection class and implement this interface for each real
          >> DB connection desired.[/color]
          >
          >If I define objects for every type of database connection, then I
          >haven't really gained anything have I? I will still have to deal with
          >a whole bunch of objects instead of just one. I guess it might be a
          >little simplier but it sounds like a lot of work for very little
          >payoff. Am I missing something?[/color]

          Yes - you define the interface in the base class, but then you override the
          database-specific parts in the subclasses, that inherit from the base class.

          You then use _exactly the same code_ in your application, regardless of what
          database you're connected to (and so which subclass you're using), using only
          the interface defined by the base class.

          The only difference is at connect time, when you specify the database you
          connect to - that's where you choose which class you instantiate; maybe the
          MySQL subclass, or maybe the MS SQL one. From then on, the actual class isn't
          relevant.

          --
          Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
          http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

          Comment

          Working...