0

Using this:

$db = mysqli_connect
(
    $db_host,
    $db_user,
    $db_pwd
) or die ("FATAL ! : The server ".$db_host." is not responding to ".$db_user."!");

how to get the $db value into this class (from include "class.php") - without errors ;-)

class User
{
    public $db;
    public $id;

    public function getUser()
    {
        if($this->id)
        {
            $sql = "
            SELECT
                users.*
            FROM
                users
            WHERE
                users.u_id='".$this->id."'
            ";
            $res = mysqli_query($db, $sql) or die(mysqli_error($db));
            $user_row = mysqli_fetch_object($db, $res);
            return $user_row;
        }
        else
        {
            return false;
        }
    }
    } // end class User

All I get is - well, nothing. Exept my main page loads empty... If I stop using the class, all is well (exept no data for the user is shown ;-))

1
  • Honestly, just create a database class and extend these models off it. No need for these extra steps. OR if you have multiple databases, setup a singleton or factory pattern and call it statically. Commented Oct 31, 2012 at 18:15

2 Answers 2

1

You must instantiate your class, and you must use the $this operator to access class members. Futhermore, it is better to inject parameters on construction and usage:

class User
{
    private $db;

    public function __construct($db)
    {
       $this->db = $db;
    }

    public function getUser($id)
    {
        if($id)
        {
            $sql = "
            SELECT
                users.*
            FROM
                users
            WHERE
                users.u_id=" . (int)$id ;

            $res = mysqli_query($this->db, $sql) or die(mysqli_error($this->db));
            $user_row = mysqli_fetch_object($this->db, $res);
            return $user_row;
        }
        else
        {
            return false;
        }
    }
 }

Usage:

$user = new User($db);
$userdata = $user->getUser(2);
Sign up to request clarification or add additional context in comments.

1 Comment

mysqli_fetch_object($db, $res); this also need $this
0

when you instantiate the class (User), if you have already assigned the variable ($db)... you can do

$user = new User;
$user->db = $db;
$user->getUser();

yes AND inside your class when you are referencing properties of the class from its methods, use the $this notation... i.e. change $db to $this->db

that should work. you mentioned something about suppressing errors, that is a different issue. I am unsure what your goal is, but normal exception handling prior to the class's instantiation would be adequate.

2 Comments

Worked like a charm. I am of course aware of a need for a more Classy (get it ;-)) correct method. Will be in a next release ..
you should note some of the classes written in other answers, I stuck only to what I thought the question to be. they are right though in trying to move the connect to inside the class. It is a more complete solution. glad you got what you were looking for tho.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.