Request:

select * from (select count(`id`) from `users` where `login` = :l) as one, (select count(`id`) from `users` where `email` = :e) as two 

Must return the number of matches. But for some reason this does not happen.

  • one
    @rnddev, most likely, something like $check['one'] is called later in the code, while the desired array is obtained as follows: $ data = $ check-> fetch (); - etki
  • @Etki is that yes, I noticed, and the request does not work for some reason as it should - Bastiane
  • one
    @rnddev, add aliases for count(id) , otherwise mysql will glue them into one field and how horrible it is. - Yura Ivanov

1 answer 1

@rnddev , if you need to check the presence of the user, then you need to use constructions like

 SELECT 1 FROM `users` WHERE `login` = :login OR `email` = :email 

As for the above query, so one and two there are aliases not of the fields, but of the tables, because the selects are listed in FROM , and the values ​​are returned in COUNT(`id`) and elements with a numeric index (unless PDO::FETCH_* ). In order for one and two to become column aliases, you need this query:

 select * from (select count(`id`) AS one from `users` where `login` = :l) as login_matches, (select count(`id`) AS two from `users` where `email` = :e) as email_matches 
  • @Etki, clearly, thanks, it helped. OR I can not use, because I need to show what is busy, login or email. - Bastiane
  • @rnddev thought it would be short, but a sheet was released SELECT 'login' AS occupied FROM users WHERE EXISTS (SELECT 1 FROM users WHERE login =: subject) UNION SELECT 'email' FROM users WHERE EXISTS (SELECT 1 FROM users WHERE email =: subject) UNION SELECT 'nothing' LIMIT 1 - etki