PHP and MySQL - Constants, Function, Best Practices? -
i've been using php , mysql years now, never in formal setting. use streamline things @ work, , have programmed lot of own commercial websites, i'm not employed programmer, , never have been. i'm looking start doing freelance work though, , i'm wondering of best practices are.
i use getter class retrieve data mysql, , setter class enter data mysql, pass connection class. every mysql table , field name has own constant defined in constants include file. though, enter constant directly setter or getter classes, rather passing them variable. makes life easier , quicker way, defy understanding of function within class being should independent.
just clear, example of do:
constants.inc.php
define('table_name','table_name');
dbase_getters.class.php
public function get_data($connection){ $query = "select * " . table_name; $result = $connection etc... }
or should doing this?
dbase_getters.class.php
public function get_data($connection, $table_name){ $query = "select * " . $table_name; result = $connection etc... }
it seems lot more hassle passing variable, because if want change parameters of function adding new field search, have find every instance of function calls , change them, whereas if use constants, need add them function.
also, when comes things declaring constants, i've used single-quotes. when create strings, use double-quotes. being inconsistent, , should use 1 type of quote time?
thanks!
you can, of course, both ways , think both equally undesirable. dynamically creating sql strings both difficult secure , error prone.
i suggest try 1 of many readily available libraries out there, example:
http://www.doctrine-project.org/
those called "orm" - object relational mappers. in achieving - believe - trying do.
there other approaches out there, once used pear db (mdb2 now, http://pear.php.net/package/mdb2) isn't high level orm. design problems.
database access common problem solve , many people have done before, use found out , don't reinvent wheel! try using orm. it's satisfying , comes more naturally handling sql, rows, resultsets , connection objects.
Comments
Post a Comment