Jump to content

getting user information from a class


garry

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/111097-getting-user-information-from-a-class/
Share on other sites

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

 

 

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.