question about best practice for coding sql statements
| From: | tamouse mailing lists | Date: | Sat, 12 Nov 2011 11:30:38 +0000 |
| Subject: | question about best practice for coding sql statements | ||
| Groups: | php.db php.general | ||
| Request: | Send a blank email to php-db+get-48095@lists.php.net to get a copy of this message | ||
I'm curious how others feel about this. When I'm creating an SQL
string, either for a non-prepared or prepared execution, I build it in
a variable and then pass the variable to the query or prepare
function. This lets me easily add something to view the SQL statement,
and also if there's an error, emit the SQL statement. Thus:
$sql = "SELECT * FROM TABLE WHERE id=$id";
if ($debug) error_log($sql." from ".__FILE__.' at '.__LINE__' in
'__FUNCTION__.PHP_EOL); // just an example
$rslt = $db->query($sql) or die("Could not get result from $sql:
".$db->errno.": ".$db->error.".".PHP_EOL);
and so on...