0

I have searched around for the answer to this but everything I try gives me an error.

I have a php function:

function _buildQueryCategoryK2Multiple(){
    $query="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17";
    return $query;
}

Basically I would like to add another line to $query, something like:

function _buildQueryCategoryK2Multiple(){
    $query1="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17";
    $query2="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18";
    return $query1.$query2;
}

If I do this though I get an error. Does anyone know the correct way to do this? Thank you

2
  • What do you really want to do (apart from appending strings) and what is the error? Commented Dec 10, 2013 at 11:44
  • What are you trying to do? Find records where the catid is either 17 or 18? Commented Dec 10, 2013 at 11:44

8 Answers 8

2

SQL IN STATEMENT

http://www.tutorialspoint.com/mysql/mysql-in-clause.htm

If you want to get 17 and 18, use SQL IN statement instead of copying whole query, what will be if you want to select 100 ids? you'll send kilobytes of queries to db ?

Example:

 function _buildQueryCategoryK2Multiple(){
//if ids you are getting in array, you can implode it to string with ',' and put the string variable instead of '17,18'
        $query="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid in (17, 18)";
        return $query;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You should separate the queries with ; One way is using implode() :

function _buildQueryCategoryK2Multiple(){

    $query[]="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17";
    $query[]="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18";
    return implode(";",$query);

}

Comments

1

The semicolon is an issue as it delineates the end of a MySQL command line. So you need to add that to the end of each line. Also while some will recommend concatenating the queries, a simpler way that is a tad more flexible—at least from my real world use of scenarios like this—is to create an array & use implode() on it. So your code like this.

function _buildQueryCategoryK2Multiple(){

    $query = array();
    $query[] = "select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17;";
    $query[] = "select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18;";

    return implode(' ', $query);

}

Another more elegant option based on the queries you have presented is to send an array of values to the function.

function _buildQueryCategoryK2Multiple($catid_array = array()){

    if (empty($catid_array)) {
        $catid_array = array(17,18);
    }

    $query = array();
    foreach ($catid_array as $catid) {
        $query[] = sprintf("select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=%d;", $catid);
    }

    return implode(' ', $query);

}

Comments

1

Add a semicolon to your $query1 here as shown

$query1="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17;";
                                                                 -----------------------------^

You need to separate multiple SQL statements using a ;

If you were using MySQL WorkBench, you could have found it.

Comments

0

You an concat the two strings with some special characters like "|" or "#" etc and return the single string.

In the calling function you can split the two string with the special character you used and use separately.

Thanks

Comments

0

Need to be add ; end of every select statment

function _buildQueryCategoryK2Multiple(){
    $query1="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and (catid=17 OR  catid=18); ";
    $query2="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18; ";
    return $query1.' '.$query2;
}

Comments

0

You can use array format as

function _buildQueryCategoryK2Multiple(){

    $returnArr = array();

    $returnArr['query1'] ="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17";

    $returnArr['query2'] ="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18";

    return $returnArr;

}

Comments

-1

You can do it like this:

function _buildQueryCategoryK2Multiple()
 {
    $query="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=17";
    $query .= " union all "
    $query .= "select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid=18";
    return $query;
    }

or

function _buildQueryCategoryK2Multiple()
{
    $query="select id,title,alias,catid,introtext FROM #__k2_items where published=1 and catid in (17,18,19) ";
    return $query;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.