How do you manage SQL Queries

后端 未结 10 1727
深忆病人
深忆病人 2020-12-13 05:59

At the moment my code (PHP) has too many SQL queries in it. eg...

// not a real example, but you get the idea...
$results = $db->GetResults(\"SELECT * FRO         


        
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 06:04

    First up, you should use placeholders in your query instead of interpolating the variables directly. PDO/MySQLi allow you to write your queries like:

    SELECT * FROM sometable WHERE iUser = ?
    

    The API will safely substitute the values into the query.

    I also prefer to have my queries in the code instead of the database. It's a lot easier to work with an RCS when the queries are with your code.

    I have a rule of thumb when working with ORM's: if I'm working with one entity at a time, I'll use the interface. If I'm reporting/working with records in aggregate, I typically write SQL queries to do it. This means there's very few queries in my code.

提交回复
热议问题