SQL query building practices

ancuh0

New Member
I am building a SQL query that dynamically changes based on passed in \[code\]$_GET\[/code\] parameters. I simply want to know if putting in a 'dummy' constraint is an 'acceptable' practice, so I do not have to check the SQL string for the existence of 'where' before I add new constraints (each time).For example, the 'dummy' constraint':\[code\]$sql = "select * from users u where u.id != 0";\[/code\]Now, in each block where I determine if I have to add more constraints, I can just do:\[code\]if (!empty($uid)) $sql .= " and (u.id = {$uid})";\[/code\]Instead of doing:\[code\]if (!empty($uid)) { $sql .= strpos($sql, "where") === false ? " where " : " and "; $sql .= " (u.id = {$uid})";}\[/code\]
 
Back
Top