Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
is it best to use a php class for mysql queries?
Old 09-29-2008, 06:21 AM is it best to use a php class for mysql queries?
Average Talker

Posts: 27
Trades: 0
hi there,

ive been working with php a little while now and have written some small basic CMS systems myself which have done their job as needed.

i use a certain amount of functions within the site (i.e. resizing images that have been uploaded, checking a valid admin user is logged in etc) but feel that i could be using them a lot more. especially with regards to areas of the CMS where i add/edit/delete mysql records as i am currently writing code for this several times over within the CMS.

i also see that people recommend using the use of classes wherever possible, such as to connect to the database and even some sites that say to use them when adding/editing/deleting mysql records too?

which is the route i should be taking? and if so, any suggestions or links to sites that show me or have samples of codes for class/functions how to do this?

many thanks
gavrd1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-29-2008, 06:38 AM Re: is it best to use a php class for mysql queries?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Simple rule:

If you have the same code more than once, make it a function (or a class)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-29-2008, 06:50 AM Re: is it best to use a php class for mysql queries?
Average Talker

Posts: 27
Trades: 0
thats what i thought too.

so with regards to when i add/delete/update a mysql record, am i best in using a class to do that?

i have managed to use a class to connect to the database sucessfully. i am now just looking for tutorials/sample code on how to use a class to add/delete/update the mysql database.

anyone know any nice easy to understand samples?

thanks
gavrd1 is offline
Reply With Quote
View Public Profile
 
Old 09-29-2008, 12:47 PM Re: is it best to use a php class for mysql queries?
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here's are two simple static functions within a class I use as helpers when doing mysql selection:
PHP Code:
class wayfarer {
    public static function 
spoon($table$sort="") {
        if(!empty(
$sort)) {
            
$sort " ORDER BY ".$sort;
        }
        
$sql="SELECT * FROM ".$table.$sort;
        
$result=self::sql($sql);
        return 
$result;
    }

    public static function 
sql($s) {
        if(!
$result=mysql_query($s)) {
            die(
SQL_MESSAGE.mysql_error());
        }
        return 
$result;
    }

I call the first one "spoon" because it is like spoon-feeding content when selecting all of the content in a table, then optionally sorting it a certain way.

The wayfarer::spoon() uses the second one, wayfarer::sql(), which is the more flexible function. All it does is do my error checking for me so I can save a single line of code on other pages. SQL_MESSAGE is a predefined constant.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to is it best to use a php class for mysql queries?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19679 seconds with 12 queries