Switch MySQL query conditions, after all results listed


what would be the easiest way to switch a mySQL query condition within PHP after all results are listed.

Let’s assume I have a table with the fields “fruit” and “color”.
What I am looking for is a loop where in the first round all fruits with red color will be selected and in the second round all as green fruits.

//first round
$fruit_color = “red”
//second round
$fruit_color = “green”

$fruit_selection = mysql_query(“SELECT fruit FROM table WHERE color LIKE ‘$fruit_color’”, $dbi);
while(list($fruit) = mysql_fetch_row($fruit_selection)){
echo “$fruit”;

Between these two rounds I would like change some parameters for other variables.

Thanks for any hint.

SELECT fruit FROM table WHERE colour IN ('red', 'green') ORDER BY colour;

This should do that for you; now you just need to figure out how to display the data in a format you require.

Thanks, but I am looking for something different. :slight_smile: (My fault)
Let me ask the question a little differnt to show the problem more precise:

I want to send some users a mail. Each mail shall be in the preferred user’s language. The language is done with constants.
To avoid redefining constants for each user (unnecessary SQL query and time), I would like to first define the English constants and than select all users that use English. After the mail has been sent to all of them, I would like to define the German constants and send the same mail in German.

Thus I need some loop that lets me redefine constants after all users of one language have been selected. So this would need more some kind of PHP solution rather than the SQL type.

So this is what I want to do:

  1. Define language
  2. Define Constants based on language
  3. Select all users which use defined language
  4. Switch language (go back to 1.) after all users of earlier defined language have been processed.

Somehow a while loop or a for each seems to be not the right type - or am I mistaken?


What i would do in this type of situation and im sure someone has a better way but i would use a function and just simply have it do all the work.

function sendMail($lang='english'){
    // Grab all the language
    // Grab all the users matching the $lang
    $res = mysql_query("SELECT someone FROM something WHERE language='$lang'");
    $userList = array();
    while($row = mysql_fetch_array($res)){
        $userList[$row['username']] = $row['email'];
    // Make sure the array has a length of at least 1
    if (sizeof($userList) == 0){
        exit('Invalid user array given or no array given at all.');
    // Setup the message
    // Would be replaced by the language constants
    $message  = "Hello %s,\
    $message .= "We would like to share something with you.";
    foreach($userList as $user => $email){
        // Parse the message
        $message = sprintf($message, $user);
        if (!mail($email, 'Subject title', $message, 'From <you@something.com>')){
            exit('Error sending email to '.$email);


Thanks, something like this should work, although I was expecting a “simple” solution without calling a function… :eye: