Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You never actually give access to the <code>PDO</code> instance <code>$dbh</code> to the things youre trying to use it in. It seems like youre using classes simple as groupings of functions but also expecting some magic to happen :-) This is how i would do it with your existingcode:</p> <pre><code>class DB_Class { protected $dsn; protected $user; protected $password; protected $connection; public function __construct($dsn = null, $user = null, $password = null) { $this-&gt;dsn = $dsn; $this-&gt;user = $user; $this-&gt;password = $password; } public function connect() { if($this-&gt;connection) { return $this-&gt;connection; } try { $this-&gt;connection = new PDO($this-&gt;dsn,$this-&gt;user, $this-&gt;password, array( PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND =&gt; "SET NAMES utf8" )); $this-&gt;connection-&gt;setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); $this-&gt;connection-&gt;setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER); return $this-&gt;connection; } catch (PDOException $e) { return false; } } } class User { protected $db; public function __construct(DB_Class $db = null) { $this-&gt;db = $db; } public function setDb(DB_Class $db) { $this-&gt;db = $db; } public function get_fullname($uid) { $stmt = $this-&gt;db-&gt;connect()-&gt;prepare("SELECT EmailAddress FROM users WHERE UserID =:username"); $stmt-&gt;execute(array(':username', $uid)); if($row = $getName-&gt;fetch()) { return $row['emailaddress']; } else { return null; } } } </code></pre>
 

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