connecting to my database
after several months sauntering about, i realize that initially i may have thrown some code together just to make it work. since many of my projects are web-based and requiring inserting into and drawing from a database, i decided to look at how i was performing the connection in my codes.
its different in different projects but it shouldn't be one would reckon. what i have settled on for now is this approach: i define a function called "db_connect()" which contains the server name, user name, password, and the database the project uses. these will not change so i can hard-code them in this function if i want. when i do need to change one, i only have to change it here. (i am still figuring out how to have a function read values from a config file so this is not currently an option).
the function performs the database connection and the database selection. it returns nothing.
i also define a function called "db_query()". this function calls "db_connect()", then calls "mysql_query(...)" which is a php function, and finally calls "mysql_close()" which is also a php function. it is actually not necessary to close the connection explicitly since it will be closed after performing the query anyway.
under this strategy, performing a database query somewhere requires only one line of code:
db_query("select * from bears where essense='wacky'");
its different in different projects but it shouldn't be one would reckon. what i have settled on for now is this approach: i define a function called "db_connect()" which contains the server name, user name, password, and the database the project uses. these will not change so i can hard-code them in this function if i want. when i do need to change one, i only have to change it here. (i am still figuring out how to have a function read values from a config file so this is not currently an option).
the function performs the database connection and the database selection. it returns nothing.
i also define a function called "db_query()". this function calls "db_connect()", then calls "mysql_query(...)" which is a php function, and finally calls "mysql_close()" which is also a php function. it is actually not necessary to close the connection explicitly since it will be closed after performing the query anyway.
under this strategy, performing a database query somewhere requires only one line of code:
db_query("select * from bears where essense='wacky'");
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home