Note that there are some explanatory texts on larger screens.

plurals
  1. POTry Catch Block in PDO wrapper Class?
    primarykey
    data
    text
    <p>I have wrote a neat PDO wrapper in class. I am trying to figure it out where is the best place to put <code>Try/Catch</code> block? In the PDO class wrapper or other files?</p> <p>For example:</p> <pre><code>public function fetchData($sql, $params = array(), $fetchMode = 'all') { try { $this-&gt;sth = $this-&gt;db-&gt;prepare($sql); if (count($params) &gt; 0) $this-&gt;setBind($params); $this-&gt;sth-&gt;execute(); $fetch = ($fetchMode == 'single') ? "fetch" : "fetchAll"; return $this-&gt;sth-&gt;$fetch(); } catch (PDOException $e) { return $data['error'] = array( 'status' =&gt; 'error', 'message' =&gt; $e-&gt;getMessage() ); } } </code></pre> <p>Or should I do like this without <code>try/catch</code> block:</p> <pre><code>public function fetchData($sql, $params = array(), $fetchMode = 'all') { $this-&gt;sth = $this-&gt;db-&gt;prepare($sql); if (count($params) &gt; 0) $this-&gt;setBind($params); $this-&gt;sth-&gt;execute(); $fetch = ($fetchMode == 'single') ? "fetch" : "fetchAll"; return $this-&gt;sth-&gt;$fetch(); } </code></pre> <p>Example Query:</p> <pre><code>try { $SQL = "SELECT * FROM table"; $result = $db-&gt;fetchData($SQL); print_r($result); } catch (PDOException $e) { $data['error'] = array( 'status' =&gt; 'error', 'message' =&gt; $e-&gt;getMessage() ); } </code></pre> <p>Update: Which would be more efficient? For example I don't want to repeat try/catch block in all functions or files.... and I still be able to return different "fail-message" on each one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload