Question

Im gonna edit the question to make it clearer, so you can see what i have got now, and get an easier understanding of the problem.

<?php
$mysqli = new mysqli("localhost", "user", "password", "test");
class building
{
private $mysqli;
public $buildingid;
public $userid;
public $buildinglevel;




public function __construct($buildingid, $userid, \mysqli $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
}

public function getLevel()
{
    return $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid."");
}
}

}
?>

Then I use this to create and use the function:

$cityHall = new building("cityHall",$user['id'],$mysqli);
echo $cityHall->getLevel();

This turns out blank, and nothing happens.

Was it helpful?

Solution

You should inject instance of mysqli to __construct() of building class:

$mysqli = new mysqli('user', 'password', 'localhost', 'test');

if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); }

class building
{
private $mysql;
private $buildingid;
private $userid;

// I need to have a mysqli_query here to get the info for the correct building, 
//to be able to set the "buildinglevel" for each object from the MYSQL DB, seems basic   
//but none of the things ive tried has worked.


public function __construct($buildingid, $userid, $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
    $this->userid = (int)$userid;
    $this->buildingid= (int)$buildingid;
}

public function getLevel()
{
    $query = $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid);
    $row = $query->fetch_assoc();
    if (!$query) {
        return $this->mysqli->error;
    }
    if ($query->num_rows == 0) {
        return 'no database records found';
    }

    return $row;
}

}

$Bulding = new building("cityHall", $user['id'], $mysqli);
$level = $Bulding->getLevel();
var_dump($level);

OTHER TIPS

Objects are units that encapsulate behavior which is exposed to other objects via methods. A wrapper around public properties retrieved from the DB does not object-oriented programming make. In fact, mysqli can do this for you via fetch_object:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}

Unless the building class actually offers functionality via methods, and implements some abstraction, it's not needed. What you can have instead is a DAO (Data Access Object) that wraps the DB (mysqli) and the data it retrieves is used by your model.

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

What your class there seems to be, is what’s known as a model: it represents some form of data, in your case a particular building.

One approach is to pass the MySQLi object as a constructor object, as well as the ID of the building you’re wanting to query for, and assign the result to class properties. This would look as follows:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

You can then fetch your building’s properties like this:

$building = new Building($mysqli, 1);

printf('Building ID is %d and building level is %d', $building->getId(), $building->getLevel());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top