By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,746 Members | 1,893 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,746 IT Pros & Developers. It's quick & easy.

eval MySQL commands

P: n/a
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!
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Steve Steward wrote:
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!


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
Jul 17 '05 #2

P: n/a
Guillaume Brocker wrote:
Steve Steward wrote:
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".

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.


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/
Jul 17 '05 #3

P: n/a
Guillaume Brocker <gu***************@ircad.u-strasbg.fr> wrote in message news:<40***********************@news.free.fr>...
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.


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($this->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. :)
Jul 17 '05 #4

P: n/a
On 20 Apr 2004 14:05:32 -0700, st***********@bhi1.com (Steve Steward) wrote:
Guillaume Brocker <gu***************@ircad.u-strasbg.fr> wrote in message news:<40***********************@news.free.fr>...
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.


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?


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.