phpezsql

Grouping WHERE conditions with ezSQL v4 PDO


How can I group my WHERE conditions when using the selecting function? I know how to write it in standard SQL, but I can't work this out using ezSQL.

What I'm wanting

SELECT user_id, user_password, user_password_key
FROM users
WHERE (user_login = $userData OR user_email = $userData) AND user_status=1

What I've tried

$db->selecting('users', 'user_id, user_password, user_password_key',
               where(eq('user_status', '1', _AND)),
               where(eq('user_login', $userData, _OR ), eq('user_email', $userData )));
$db->selecting('users', 'user_id, user_password, user_password_key',
               where(eq('user_status', '1')),
               where(eq('user_login', $userData, _OR ), eq('user_email', $userData )));
$db->selecting('users', 'user_id, user_password, user_password_key',
               $db->where(eq('user_login', $userData, _OR ), eq('user_email', $userData )),
               $db->where('user_status', '1'));
$db->selecting('users', 'user_id, user_password, user_password_key',
               eq('user_login', $userData, _OR ), eq('user_email', $userData ),
               $db->where('user_status', '1') );

It works if I remove the condition for the user_status, but when it's in there it just returns NULL.

Examples copied from https://github.com/ezSQL/ezSQL#example-for-using-prepare-statements-indirectly-with-above-shortcut-sql-methods


Solution

  • This is now possible with v4 using the following code

    $db->selecting(
      'users',
      'user_id, user_password, user_password_key',
      where(
        eq('user_status', '1'),
        grouping(
          eq('user_login', $userData, _OR),
          eq('user_email', $userData)
        )
      )
    );
    

    More details on the GitHub Wiki