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 8839669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:17:18+00:00 2026-06-14T10:17:18+00:00

I was wondering how I could make a general function for SELECT mysql queries

  • 0

I was wondering how I could make a general function for SELECT mysql queries from my SelectQuery object in PHP. SelectQuery extends Query, which means it inherits the database connection, a realescape method (which is mysqli_real_escape_string()), and a query method which executes the query. Besides that, it also gets a protected variable called _sql, which is the SQL the query() method passes to the database. And it also gets a protected variable called _table, which contains the (escaped) name of the table it’s working on.

My code:

public function select($columns = array('*'), $known = null, $limit = null, $offset = null, $orderby = null, $asc = true) {
    if (!is_array($columns)) {
        new Error('Parameter is not an array.');
        return;
    }

    $select = '';
    foreach($columns as $column) {
        $select .= (($select != '')?', ':'') . '`' . $this->realescape($column) . '`';
    }
    $conditions = '';
    if (is_array($known)) {
        foreach($known as $column => $value) {
            $conditions .= (($conditions != '')?' AND ':'WHERE ') . '`' . $this->realescape($column) . '` = ' . ((is_string($value))?'\'':'') . $this->realescape($value) . ((is_string($value))?'\'':'');
        }
    }
    $domain = '';
    if ($limit !== null) {
        $domain = 'LIMIT ' . $this->realescape($limit);
        if ($offset !== null) {
            $domain .= ' OFFSET = ' . $this->realescape($offset);
        }
    }
    $order = '';
    if ($orderby !== null) {
        $order = 'ORDER BY `' . $this->realescape($orderby) . '` ' . (($asc)?'ASC':'DESC');
    }

    $this->_sql = 'SELECT ' . $select . ' FROM `' . $this->_table . '`';
    if ($conditions != '') {
        $this->_sql .= ' ' . $conditions;
    }
    if ($domain != '') {
        $this->_sql .= ' ' . $domain;
    }
    if ($order != '') {
        $this->_sql .= ' ' . $order;
    }
    return $this->query();
}

The $known variable might be set, if set it should be an array which contains all the ‘known’ elements of the rows we are selecting.

My question: How can I make this so that conditions such as
age < 18, or date > 5120740154 are easily made?

Also, if you think the way I’m making this work is wrong, please say so.

Thanks in advance

  • 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-06-14T10:17:19+00:00Added an answer on June 14, 2026 at 10:17 am

    Dynamically build WHERE conditions that can use any operators

    Create a $where array that holds your where conditions.

    $where = array (
        'age <' => 18,
        'date >' => 5120740154
    );
    

    You can build that into your SQL query using a function like this:

    private function buildWhereConditions($where) {
        $conditions = 'WHERE 1=1';
        /*1=1 is just so you can easily append ANDs */
        foreach ($where as $key => $value) {
            $conditions .= " AND $key $value ";
        } 
        return $conditions;
    }
    

    This will return the where conditions to be appended to your query:

    WHERE 1=1 AND age < 18 AND date > 5120740154
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I was wondering I could make my flash program take input from an
I was wondering how you could make it so that website.com has an index.php
this is a general-purpose way to make GET requests with jQuery: var loadUrl="mypage.php"; $("#get").click(function(){
I was wondering if I could make a large number of system calls at
I was wondering if someone could explain to me how to make a simple
I was wondering which could be a better way of mounting different apps for
I was wondering how could I remove one (wrongly inserted) entry/word from the ispell
alt text http://img202.imageshack.us/img202/2637/dialogo.png I'm wondering how I could make this into a popup dialog.
I was wondering how one could make codeigniter style url segments on a project.
Hey, I was wondering how I could make the user being able to change

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.