0

I need to select all from database when "main=1" once, then select all from database when "main=0" and use them within the same request. Can I do this in one request? this is the request

1.

$result = $apt->query("SELECT * FROM cat where main='0'");

while($row=$apt->dbarray($result)){

@extract($row);

2.

$result = $apt->query("SELECT * FROM cat where main='1'");

while($row=$apt->dbarray($result)){

@extract($row);

I kind of need to combine them in one request because this will make a conflict when do 2 request. Thanks

1
  • you can create function where you can pass the value of conditional parameter "main" Commented Jun 15, 2013 at 4:36

2 Answers 2

2

You have a couple of options

SELECT * from cat where main = '0' or main ='1'; 

or

SELECT * from cat where main in ('0','1');

If what you meant by a conflict is in reference to maintaining concurrency between the two queries, this way and wrapping the entire operation in a transaction are the only ways to ensure concurrency.

Here is an example of how you can use the queries above to emulate the code you provided but doing it in a single query.

  $result = $apt->query("SELECT * FROM cat where main='0' or main = '1' order by main");
  while($row=$apt->dbarray($result)){
        @extract($row);
        switch ($main){
            case 0:
                // do stuff for when main = 0
                break;
            case 1:
                // do stuff for when main = 1
                break;
        }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your replay, But I need to use main as 1 once then use it as 0 once within the same request. Any help?
it appears as though that is what you are doing in your question. You seem to be affraid of some kind of conflict... can you describe a situation where such a conflict would arise?
1

1) Create 1 array and insert the result inside that array.

$array = array();
$result = $apt->query("SELECT * cat where main='0'");

while($row=$apt->dbarray($result)){
    $array['main0'][]  = $row[];
}

$result = $apt->query("SELECT * cat where main='1'");

while($row=$apt->dbarray($result)){
    $array['main1'][]  = $row[];
}

echo json_encode($array);

2) OR you can use 1 query with IN

$result = $apt->query("SELECT * cat where main IN ('0','1')");

while($row=$apt->dbarray($result)){
    $array['main0'][]  = $row[];
}

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.