1

I want to retrieve the amount of employees all the companies have, but it doesn't call the function.

This is my Class:

class Rimaxx {

public $host = "";
public $username = "";
public $password = "";
public $database = "";

public function GetCompanies()
{
$conn = new mysqli ($this->host, $this->username, $this->password, $this->database);

$sql = "SELECT * FROM freelancers";
$result = $conn->query($sql);

return $result;

$conn->close();

}

public function CountEmployees($id)
{
$conn = new mysqli ($this->host, $this->username, $this->password, $this->database);

$sql = "SELECT * FROM Werknemers WHERE Idbedrijf = '$id'";
$result = $conn->query($sql);

return $result->num_rows;

$conn->close();
}

And here is where I define things:

include('rimaxx.php');

$rimaxx = new Rimaxx();

$rimaxx->host = "23.12.12.32";
$rimaxx->username = "xxx";
$rimaxx->password = "xxxxxx";
$rimaxx->database = "rimaxx";

$companies = $rimaxx->GetCompanies();

And here is my while loop:

 <?php while($row = $companies->fetch_assoc()) { ?>
            <tr>
              <td><?php echo $row["Bedrijf"]; ?></td>
              <td><?php echo $row["Plaats"]; ?></td>
              <td><?php echo $row["Postcode"]; ?></td>
              <td><?php echo $row["Provincie"]; ?></td>
              <td><?php echo $rimaxx->CountEmployees($row["Idbedijf"]); ?></td>
            </tr>
 <?php }; ?>

Please, can some body help my?

9
  • I think you called the wrong function because the class methode $companies->fetch_assoc() doesn't exist in your class. Can you try this: $row = $companies->GetCompanies() Commented Mar 21, 2016 at 8:12
  • @C0dekid: i assume there is a line before the loop like this: $comapnies = $rimaxx->GetCompanies(); Commented Mar 21, 2016 at 8:15
  • $companies refers to $rimaxx->GetCompanies(); Commented Mar 21, 2016 at 8:15
  • @Kylian, are you sure that the method is not called? Just echo some string inside to see. Maybe there is just a typo, you used $row["Idbedijf"] in the call and "Idbedrijf" (additional "r") inside the method Commented Mar 21, 2016 at 8:17
  • 1
    Btw, your code looks vulnerable to SQL Injection. en.wikipedia.org/wiki/SQL_injection Commented Mar 21, 2016 at 8:19

2 Answers 2

2

There is a typo in your method call, change it to this:

$rimaxx->CountEmployees($row["Idbedrijf"]);
Sign up to request clarification or add additional context in comments.

Comments

1

This is a recommendation for your code, better performance on the cycles, added the connection on the constructor

class Rimaxx
{
    protected $conn;

    public function __construct($host, $username, $password, $database)
    {
        $this->conn = new mysqli ($host, $username, $password, $database);
    }

    public function GetCompanies()
    {
        $sql = "SELECT * FROM freelancers";
        $result = $this->conn->query($sql);
        return $result;
    }

    public function CountEmployees($id)
    {
        $sql = "SELECT * FROM Werknemers WHERE Idbedrijf = '$id'";
        $result = $this->conn->query($sql);
        return $result->num_rows;
    }

    public function close(){
        $this->conn->close();
    }
}


$rimaxx = new Rimaxx  ('23.12.12.32', 'xxx', 'xxxxxx', 'rimaxx');

//.....


$rimaxx->close();

Comments

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.