Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:31:45+00:00 2026-05-27T02:31:45+00:00

I have this query which I want to run in my PHP application back

  • 0

I have this query which I want to run in my PHP application back end. Notionally, sheet is a DB that keeps track of all the sheets we have. Purchases is a DB that keeps track of which users have access to which sheet. The query I want to run is given a user’s id, I can get all the sheets that they should have access to. In query form:

select distinct s.wsid, s.name from sheets s, purchases p where 
s.wsid = p.wsid AND p.uid = *value*; 

where value is something input by the application

The way I see it there are two ways to go about getting this to work in the back end.

Option 1)

public function getPurchasedSheets($uid){
    if( is_numeric($uid) ){ //check against injections
        $query = "select distinct s.wsid, s.name from sheets s, purchases p 
            where s.wsid = p.wsid AND p.uid = ".$uid.";" ;
        return $this->db->query($query);
    } else {
        return NULL; //or false not quite sure how typing works in PHP
    }
}

Option 2)

public function getPurchasedSheets($uid){
    if( is_numeric($uid) ){ 
        $this->db->select('wsid, name'); 
        $this->db->distinct();
        $this->db->from('purchases');
        //not sure which order the join works in...
        $this->db->join('sheets', 'sheets.wsid = purchases.wsid');
        $this->db->where('uid ='.$uid);
        return $this->db->get();
    } else {
        return NULL; 
    }
}

Source for all the CodeIgniter Active Record commands:

codeigniter.com/user_guide/database/active_record.html

Is there some sort of performance or security difference from doing thing one way or another? Doing it the second way seems so much more confusing to me… This is compounded a little bit because I am not sure how to do referential disambiguation in this style of coding because purchases and sheets both have a uid field but they mean different things (in addition to not being very familiar with the SQL join command in the first place.). Uid (user id) in purchases means that user has purchased that sheet, while Uid in sheets denotes which user owns that sheet.

TL,DR: Basically, I am asking is there a reason I should sink time into looking how to do things the option 2 way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:31:46+00:00Added an answer on May 27, 2026 at 2:31 am

    The main benefits are:

    • Abstraction from the database engine, where the library can take care
      of database-specific SQL syntax differences for you. Relevant if you
      ever have/want to change the database you’re working with. In theory,
      the second form should still just work.
    • The ‘Active Record’ syntax
      automatically escapes parameters for you.
    • Readability, although
      that’s a matter of taste.

    Incidentally, if you’re in a PHP 5 environment, the library supports method chaining:

    if( is_numeric($uid) ){ 
            return $this->db->select('wsid, name')
                                  ->distinct()
                                  ->from('purchases')
                                  ->join('sheets', 'sheets.wsid = purchases.wsid')
                                  ->where('uid ='.$uid)
                                  ->get();
                                  // nb. didn't check your join() syntax, either :)
        }
    

    Probably off-topic: CodeIgniter’s Active Record is more of a query builder than an implementation of Active Record. In case you’re wondering. When viewed as a query builder, it makes a bit more sense 😉 FWIW, I do love CodeIgniter. I jest from affection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query statement and want to only get records that has a
I have this query which works correctly in MySQL. More background on it here
Right now I have this SQL query which is valid but always times out:
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have this query. It matches anything which has South in its name. But
I have a query which produces a list of orders like this: Invoice Description
So I have this app which needs to query entities from the Azure Tables
I have an entity which might have a parent entity. I want to run
I have access to some images that can be gathered like this: http://www.example.com/imageGetter.php?a=0001 If
We currently have some php code that allows an end user to update records

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.