Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Change the function to this:</p> <pre><code>function run_db($sqlcom,$exe){ $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass'); $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn-&gt;prepare($sqlcom); $stmt-&gt;execute($exe); return $stmt; } </code></pre> <p>and the call to that function to:</p> <pre><code>try { $stmt = run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' =&gt; 'published')); while ($result = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) { $contents = $result['content']; $title = $result['title']; </code></pre> <hr> <p><strong>EDIT:</strong> Better solution is the one that <strong>jeroen</strong> advices - to <strong>return all the fetched objects at once</strong>:</p> <pre><code>function run_db($sqlcom,$exe){ $conn = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass'); $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn-&gt;prepare($sqlcom); $stmt-&gt;execute($exe); return $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); } </code></pre> <p>Then calling this way:</p> <pre><code>try { $data = run_db('SELECT * FROM posts WHERE status= :published ORDER BY id DESC LIMIT 5',array(':published' =&gt; 'published')); foreach($data as $result) { $contents = $result['content']; $title = $result['title']; </code></pre> <hr> <p><strong>EDIT 2:</strong> Anyway - wrapping such a logic into one function is not very good idea. now You are limited with executing of only <code>SELECT</code> queries and the resulting array containing always only record's associative array. What if You would like to (for any reason) retrieve the array of <strong>objects</strong>, or even <strong>only one single value</strong>? What if You would like to execute <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code> queries???</p> <p>If You for sure want to go this way, then I'd suppose creating a class with functions like this:</p> <pre><code>class MyPDO { private $connection; static $instance; function __construct() { $this-&gt;connection = new PDO('mysql:host=localhost;dbname=dbname', 'usr', 'pass'); $this-&gt;connection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } static function getInstance() { return self::$instance ? : self::$instance = new MyPDO; } // retrieves array of associative arrays function getAssoc($sqlcom, $exe) { $stmt = $this-&gt;connection-&gt;prepare($sqlcom); $stmt-&gt;execute($exe); return $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); } // retrieves array of objects function getObj($sqlcom, $exe) { $stmt = $conn-&gt;prepare($sqlcom); $stmt-&gt;execute($exe); return $stmt-&gt;fetchAll(PDO::FETCH_OBJ); } // retireves one single value, like for SELECT 1 FROM table WHERE column = true function getOne($sqlcom, $exe) { $stmt = $conn-&gt;prepare($sqlcom); $stmt-&gt;execute($exe); return $stmt-&gt;fetchColumn(); } // just executes the query, for INSERT, UPDATE, DELETE, CREATE ... function exec($sqlcom, $exe){ $stmt = $conn-&gt;prepare($sqlcom); return $stmt-&gt;execute($exe); } } </code></pre> <p>Then You can call it this way:</p> <pre><code>try { $pdo = MyPDO::getInstance(); foreach($pdo-&gt;getAssoc('MySQL QUERY'), array($param, $param)) as $result) { print_r($result); } } catch(\Exception $e) { // ... } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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