oop - How do you manage database connections in php? -


So recently I actually started using PHP actively, and I used to use the database connection Want some insights on various ways

At first I have used simple mysql_connect ():

  & lt ;? Php $ connection = mysql_connect (DB_Host, DB_USER, DB_ PASS) or die (mysql_error ()); Mysql_select_db (DB_DB, $ connection); ? & Gt; After some time, I created a database class that I started to add and start in every file - something like this:  
   Connection); } Function query ($ q) {$ res = mysql_query ($ q, $ this-> connection) or die (mysql_error ()); Return $ res; }} $ Database = new MySQL_DB; ? & Gt;  

And at this time I'm using - and it's working fine - but there are always ways to improve.

So you have my question that how do you manage your database connection?

  • Do you use classes?
  • What's in your classes (just connection or function?)
  • Which exercises do you recommend?
  • Using classes is a way to increase customizable reusability.

    Get all the normal implementation in the classroom. You are on the right track. Possible modules are as follows: // DBX_MYSQL, DBX_ODBC, DBX_PGSQL, DBX_MSSQL, DBX_FBSQL, DBX_SYBASECT, DBX_OCI8, DBXSquite Private $ Module = DBXMYSQL; Private $ host = "localhost"; Private $ database = "test"; Personal $ username = "testuser"; Private $ password = "testpass"; Private $ link; Private $ results; Public $ sql; Function __construct ($ database = "") {if (! Empty ($ database)) {$ this- & gt; Database = $ database; } $ This- & gt; Link = dbx_connect ($ this-> module, $ this-> host, $ this- & gt; database, $ this- & gt; username, $ this- & gt; password); $$ Return- & gt; Link; // makes the connection wrong if the connection can not be made. } Function query ($ sql) {if ((empty ($ sql)) {$ this-> sql = $ sql; $ this-> results = dbx_query ($ this-> link, $ sql, DBX_RESULT_UNBUFFERED; return $ this-> result;} other {return false;}} function fetch ($ result = "") {if (empty ($ result)) {$ result = $ this-> } Return dbx_fetch_row ($ result);} function __destruct () {dbx_close ($ this-> link);}}


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -