title: Php pdo calling functions and accessing variables
body: <p>I'd like some help understanding how to call the results of a function on separate pages within my simple web app. I'm quite new to php/pdo.</p>
  
  <p>Most of the time I just want to create a simple <code>SELECT</code> function and display the data on another page.</p>
  
  <p>I have a page containing all of my functions named <code>class.crud.php</code>, the contents are as follows (minified for display purposes);</p>
  
  <p><strong>class.crud.php</strong></p>
  
  <pre><code>class crud {
  
      private $db;
  
      function __construct($DB_con) {
          $this-&gt;db = $DB_con;
      }
  
      public function testing() {
          $stmt = $this-&gt;db-&gt;prepare("SELECT * FROM users WHERE user_id=:userId");
          $stmt-&gt;bindparam(":userId", $_SESSION['user_session']);
          $stmt-&gt;execute();
          $results = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
          //var_dump($results); this dumps the results successfully on the desired page
          if ($stmt-&gt;rowCount() &gt; 0) {
              while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {
                  print($row['user_id']);
                  print($row['user_name']);
                  echo $row['user_id'];
                  echo $row['user_name'];
              }
          } else {
              echo 'nothing here';
          }
          return true;
      }
  
  }
  </code></pre>
  
  <p>How do I display the results of this <code>testing()</code> function on any other page? I have tried the following but it doesn't work (no data displayed at all, although if I uncomment the <code>var_dump($results)</code> I can see an array of data displayed);</p>
  
  <p><strong>user-details.php</strong></p>
  
  <pre><code>&lt;div&gt;
    &lt;p&gt;User Details&lt;/p&gt;
    &lt;?php $crud-&gt;testing();?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>A few questions; </p>
  
  <ul>
  <li>If I <code>echo</code> or <code>print</code> variables within my functions - how do I access this data on another page?</li>
  <li>Should I <code>return true</code> or <code>return</code> anything at all?</li>
  <li>Should I format the data within my functions - i.e tabularise it. Or leave this to the page where I want it displayed?</li>
  </ul>
  
  <p>My database connection is fine and I have many functions that work. However they are very simple functions that I have been using for testing. </p>
  
  <p>Any help is appreciated in understanding the logic. I have read so many tutorials :/</p>