garry Posted June 20, 2008 Share Posted June 20, 2008 I have a class and I want it to be so that when I initiate it and give it a user_id it will get me all the information for that id. class userinfo { public $username; public $firstname; public $lastname; public $id; protected $connection; public function info($id) { $mysqli = database::connect(); $result = $mysqli->query("SELECT * FROM users WHERE id = '$id'"); $row = $result->fetch_assoc(); $this->username = $row['username']; $this->firstname = $row['firstname']; $this->lastname = $row['lastname']; } } Then when I'm trying to initiate it i'm using this but i'm not sure what's wrong $userinfo = new userinfo; $userinfo->id = $_SESSION['user_id']; How can I do what i'm trying to do? Thanks! Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted June 20, 2008 Share Posted June 20, 2008 Garry: Like this: $_SESSION['user_id'] = 'abc'; $userinfo = new userinfo; $userinfo->info($_SESSION['user_id']); With PHP5, you could define function __construct($id) { // SQL statements } or in php4 define a function with the same name as your class: function userinfo($id) { // SQL statements }, the it gets called at class instantiation time... Vis.: $_SESSION['user_id'] = 'abc'; $userinfo = new userinfo($_SESSION['user_id']); Scot L. Diddle, Richmond VA Quote Link to comment Share on other sites More sharing options...
garry Posted June 20, 2008 Author Share Posted June 20, 2008 I tried the constructor thing as well but nothing appears when I try it. I'm using php5 <?php class userinfo { public $username; public $firstname; public $lastname; public $id; public $usersonline; public $registeredmembers; protected $connection; protected $userinfo; public function __construct($id) { $mysqli = database::connect(); $result = $mysqli->query("SELECT * FROM users WHERE id = '$id'"); $row = $result->fetch_assoc(); $this->username = $row['username']; $this->firstname = $row['firstname']; $this->lastname = $row['lastname']; } } ?> That's the function and then this is what I'm using to initiate it: $userinfo = new userinfo($_SESSION['user_id']); $firstname = $userinfo->firstname; And when echoing the $firstname variable it just comes up blank :/ Do you know why? Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted June 20, 2008 Share Posted June 20, 2008 Garry, This works: <?php class userinfo { public function __construct($id) { //$mysqli = database::connect(); //$result = $mysqli->query("SELECT * FROM users WHERE id = '$id'"); //$row = $result->fetch_assoc(); $this->username = 'ScotDiddle'; $this->firstname = 'Scot'; $this->lastname = 'Diddle'; } } $userinfo = new userinfo($_SESSION['user_id']); $firstname = $userinfo->firstname; echo $firstname . "<br /><br /> \n"; ?> Which tells me that the problem is with your SQL processing... Try adding some error checking: Vis.: <?php class userinfo { public $username; public $firstname; public $lastname; public $id; public $usersonline; public $registeredmembers; protected $connection; protected $userinfo; public function __construct($id) { $mysqli = database::connect(); // Garry, Where are your User name / PW ??? Is this causing your problem ? if ($mysqli) { // Good Connect $result = $mysqli->query("SELECT * FROM users WHERE id = '$id'"); if ($result) { // Good Query $row = $result->fetch_assoc(); if ($row) { $this->username = $row['username']; // Turn on for debug display. Comment out when satisfied with the returned value. echo '$this->username : ' . $this->username . "<br /> <br /> \n"; $this->firstname = $row['firstname']; // Turn on for debug display. Comment out when satisfied with the returned value. echo '$this->firstname : ' . $this->firstname . "<br /> <br /> \n"; $this->lastname = $row['lastname']; // Turn on for debug display. Comment out when satisfied with the returned value. echo '$this->lastname' . $this->lastname . "<br /> <br /> \n"; } else { echo "Bad Row returned : " . "<br /> <br /> \n"; if (is_array($row)) { foreach($row as $rowReturned) { echo $rowReturned . "<br /> <br /> \n"; } } if (is_string($row)) { echo $row . "<br /> <br /> \n"; } if (!isset($row)) { echo "\$row is not set" . "<br /> <br /> \n"; } if ($row == NULL) { echo "\$row is NULL" . "<br /> <br /> \n"; } } } // END if ($result) { // Good Query else { echo 'Query Failed' . "<br /> <br /> \n"; } } // END if ($mysqli) { // Good Connect else { echo 'Did not connect to the database' . "<br /> <br /> \n"; } } // END public function __construct($id) } // END class userinfo { $userinfo = new userinfo($_SESSION['user_id']); $firstname = $userinfo->firstname; echo $firstname . "<br /> <br /> \n"; ?> Scot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.