MySQL connection: globally or in object?

后端 未结 4 1342
逝去的感伤
逝去的感伤 2021-01-18 07:23

I have two PHP classes. One is for connecting to the database, building queries, executing them, and disconnecting from the database. The other class is for users: adding th

4条回答
  •  遥遥无期
    2021-01-18 07:50

    In PHP, the best practice is to take the global approach. This is mainly due to the fact that repeated connects/disconnects to the MySQL server can cause a significant decrease in performance.

    In fact, though this may seem counter-intuitive, most PHP experts (including myself) recommend that you avoid using mysql_close() altogether, unless there's a pressing reason not to. This is because that is handled automatically in PHP's cleanup anyway, so adding mysql_close() just creates a further deterioration in performance.

提交回复
热议问题