In nearly every script/function I use that involves operating on the database, I use this code:
$conn = connect();
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$sql = '...';
$stid = oci_parse($conn, $sql);
oci_execute($stid);
oci_fetch_all(...);
oci_close($conn);
The $sql and occassionally the method of fetching varies, but the rest remain 100% the same in all cases I know of. Is this the proper way to do DB operations via PHP? If not, how can I avoid this boilerplate code?
You could go down the OO route. eg...
Then your code can just instantiate this class and pass in the sql. So in your script...
If the method of the fetch varies then you can just add some new functions to your class to reflect this.