php - Fatal error: Call to a member function bindValue() on a non-object in -
php - Fatal error: Call to a member function bindValue() on a non-object in -
i got error showing above. have looked on website, can't find right prepare solve problem.
i trying write user-class. next code code far.
class user { private $_id; public function __construct($id) { $this->_id = $id; } public function getusername() { global $db; $query = $db->prepare("select * users id = ?"); $query->bindvalue(1, $this->_id); $query->execute(); } } the result next error , don't know how prepare this...
fatal error: phone call fellow member function bindvalue() on non-object in
edit:
here how using it:
$user = new user($_session['logged_in']); $username = $user->getusername(); sidenote: session_start(); loaded.
using global variable poor approach.
read blog entry:
global variables bad
it explains why not idea.
if $db null, prepare not homecoming statement object.
then $query not statemnt, hence bindvalue wont recognize.
instead pass connection class, , stop using globals.
and function not returning data, modify homecoming username.
class user { private $_id; private $db; public function __construct($id, $conn) { $this->_id = $id; $this->$db = $conn; } public function getusername() { $query = $this->db->prepare("select username users id = ?"); $query->bindvalue(1, $this->_id); $query->execute(); $data = $query->fetch(); homecoming $data['username']; } } usage:
session_start(); $id = $_session['logged_in']; $conn = new pdo('xxx'); $user = new user($id, $conn); $username = $user->getusername(); php pdo bindvalue
Comments
Post a Comment