Re: MySQL class. Thoughts?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 21, 2009 at 09:10:54PM +0100, Jochem Maas wrote:

> Paul M Foster schreef:
> > On Wed, Jan 21, 2009 at 11:37:07AM -0600, Jay Moore wrote:
> >
> >> This is a MySQL class I use and I wanted to get everyone's thoughts on
> >> how/if I can improve it.  This is for MySQL only.  I don't need to make
> >> it compatible with other databases.  I'm curious what you all think.
> >>
> 
> I'd try to move to using the mysqli functions or class, which
> you can still wrap in a custom object (I do this because
> I like to minimize the interface to the bare, bare minimum ...
> professional laziness you might say).
> 
> does you class need to be php4 compatible? I'd hope
> not but you may still have to support php4 ... even then
> I'd doubt you'd be using php4 for new project so it
> might be worth making a new php5 only class.

I always write with PHP4 in mind. You never know. Besides, I can always
change the internal implementation, if it's a class.

<snip>

> 
> > A couple of thoughts. First precede all your mysql_* calls with the at
> > sign (@) to shut up the routines if they generate text. I had this
> > problem, and that was the answer.
> 
> yes that's very bad advice. using error suppression is bad for performance
> and debugging, don't do it unless you really really have to (e.g. you
> have some function that spits warnings even with display_errors set to Off)
> 
> display_errors should be set to Off in production, and errors/warnings
> shouldn't
> be suppressed, they should be logged.
> 
> handle errors gracefully ('or die()' is not graceful)

I don't know about performance. But every call to the query() method of
my class tests to see the results of the query. If it failed, I call the
proper function from PostgreSQL to fetch the error, and store it in the
class. The query function returns false, if there's an error. So the
user can test the return and then call a function to echo the stored
error.


> 
> > Second, store your connection resource as a class variable, so you can
> > pass it around to the various routines. Actually, you're already doing
> > this, but I prefer to do so explicitly, as:
> >
> > var $link;
> 
> that's very php4.

Yep.

> 
> > at the top of the class.
> >
> > I have a similar class for PostgreSQL. I also have routines like
> > "update", which allow you to pass a table name and an associative array
> > of field values. Same thing for an "insert" routine.
> 
> if the postgres extension is anything like the firebird extension then
> there may actually be a few calls which do require error suppression :-)
> 

Probably. When I first started coding PHP/PostgreSQL, I was getting
stuff printed out from the pg_* routines on the webpages and screwing
things up. I was on the PostgreSQL list at the time, and sent an email
to the list about "chatter" from the pg_* routines. The solution was to
prefix them with @. Works great, but I *do* check for and capture error
text and such.

Paul

-- 
Paul M. Foster

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux