Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    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

  2. #2
    Regular Coder
    Join Date
    Nov 2012
    Thanked 13 Times in 13 Posts
    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);
    $result) {
    $this->log_error($this->db->error "Full query: ".$sql);
      } else {

    // 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.


  3. Users who have thanked Thyrosis for this post:

    Oatley (11-30-2012)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts