Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2012, 02:45 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 76
Thanks: 61
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
OOP: Writing a method to set a query

Hello, I am looking into the possibility of writing a generic Query() method which will be used to pass in parameters. for example, a table name,field names, WHERE clauses and so on.

Now I can do this in it's simplest form to create things like select name from table where id = 1 but when it comes to adding things like being able to add things like different types joins and more in-depth queries I'm not sure how to go about it.

Would it be best to pass in things such as joins through parameters, as that seems like a lot of data to pass in to me, or is there a better way around this? Would anyone be able to suggest any links and examples that my help me here?

Thank you
Oatley is offline   Reply With Quote
Old 11-27-2012, 03:33 PM   PM User | #2
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 92
Thanks: 4
Thanked 12 Times in 12 Posts
Thyrosis is on a distinguished road
Hi Oatley,

My best guess would be NOT to do it. Personally I don't see the point of spending all the extra effort to find out how this would be possible.

Don't get me wrong, I'm all up for a Query-method, I use it myself all the time:

PHP Code:
function query($sql) {
  
$result $this->db->query($query);
  if(!
$result) {
    
$this->log_error($this->db->error "Full query: ".$sql);
    return 
FALSE;
  } else {
    return 
TRUE;
  }
}

// disclaimer: I've just typed this up without checking if I did it right as I'm at work. Please forgive any typo's or naming. 
As you can see, this saves me from logging every single query error seperately, as my objects query function handles this. The only variable being passed through is the SQL query itself, which is built up within the object and then passed through to this function. Big plus is building the query directly at the place where you need the result, thus not having to worry about passing through different variables, query types (SELECT, UPDATE), joins and all that.

Regards,
Martin
Thyrosis is offline   Reply With Quote
Users who have thanked Thyrosis for this post:
Oatley (11-30-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:48 AM.


Advertisement
Log in to turn off these ads.