In one of the vacancies for the junior php position, among other requirements, I met this: "you are suitable for us if there is no hint of mysql_query in your code (" SELECT * .... "

What is bad mysql_query ()? Is it unprofessional? How to write "correctly"? Thank.

3 answers 3

I do not know what was meant specifically for the vacancy, but I can assume that the principal importance was attached not so much to mysql or mysqli to the driver, and not even to PDO, as the candidate was supposed to have the knowledge and experience with any ORM (and a consequence, at least minimal experience with any framework, or the ability to connect the packet of interest through the composer), which represent a much higher level of abstraction and an object-based approach to data access.

Bad tone, again, is not mysql_query itself, but mindlessly mixing the logic of working with data with the logic of the application and business logic. When there is something like in the controller:

SELECT * FROM products; 

This is a bad tone. Using ORM, you will have something like:

 $productMapper = new ProductMapper(); $products = $productMapper->findAll(); 

And this approach is much more flexible and understandable (if used correctly, of course).

Read about the most common ORM for PHP ( Doctrine, Eloquent, Yii \ ActiveRecord ), see how it is common to work with them, at least briefly familiarize yourself with object-relational mapping patterns such as Table Gateway, Row Gateway, Active Record, Data Mapper , and Fight employers with your deep understanding of the subject matter.

    mysql_query is no longer in version 7. Use PDO or at least mysqli_query

    • Well, mysqli - yes, that is understandable. But it is not clear why the request itself is so bad ... If I use only one database, and I don’t need to switch between databases, why do I need a PDO? - humster_spb

    this is already a bad tone) since php 5.6 is not recommended to be used, and in php 7 just do it. need to use pdo