2

I have a class Club{} that has several public functions and private functions. The three private functions I have are used to get data from my database. Here is the following code:

private function Get_Members_From_DB()
    {
        $sql = "SELECT
                    Email, FirstName, LastName, Gender
                FROM
                    member";

        $result = mysqli_query($this->Con, $sql);

        $arrayResult = array();

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

    private function Get_Members_Interests_From_DB($MemberEmail)
    {
        $sql = "SELECT 
                    interest_type.InterestDescription
                FROM
                    member, member_interests, interest_type
                WHERE
                    member.Email = '$MemberEmail'
                AND
                    member.Email = member_interests.Email
                AND
                    member_interests.InterestID = interest_type.InterestID";

        $result = mysqli_query($this->Con, $sql);

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

    private function Get_Interests_Types_From_DB()
    {
        $sql = "SELECT 
                    InterestID,
                    InterestDescription
                FROM
                    interest_type";

        $result = mysqli_query($this->Con, $sql);

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

When I call these, I get Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 406, which is while($row = mysqli_fetch_array($result)). I also get Fatal error: Cannot use object of type Club as array in on line 48 which is echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] .

I have tried several versions of this, and I cannot seem to get the data to work into the following function:

public function DisplayMembers()
    {   
        //$members[] = array();
        $interests[] = array();
        $members = new Club();
        $members->Get_Members_From_DB();

        echo ("<table id='membertable'><tr><td colspan='4'>Informatics Club Members</td></tr>
                <tr><td width='130px'>Name</td><td width='170px'>Email</td><td width='60'>Gender</td>
                <td width='280px'>Interests</td></tr>");

        while($row1 = $members)
        {
            echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] . 
                    "</td><td>" . $row1['Gender'] . "</td><td><ul>;");

            $results2 = new Club;
            $results2->Get_Members_Interests_From_DB($row1['Email']);

            while($row2 = mysqli_fetch_array($results2))
            {
                $interests[] = $row2;
                echo("<li>" . $row2['InterestDescription'] . "</li>");
            }

            echo("</ul></td></tr>");
        };

        echo "</table><br>";
}

I had changed private function Get_Members_From_DB() to:

$sql = "SELECT
                    Email, FirstName, LastName, Gender
                FROM
                    member";

        $result = mysqli_query($this->Con, $sql); return ($result);

And it still does not work in the DisplayMembers() function with out getting the first error I posted above, except it would be on line 46, which would be changed to: while($row1 = mysqli_fetch_array($members)) If I take the above code and paste it directly into DisplayMembers() I can get the function to display correctly as an HTML table. I do not understand why I am getting errors when I separate the code into the private function. Any ideas?

EDIT: I am calling these private functions from a public function inside the SAME class.

8
  • Did you try public function Get_Interests_Types_From_DB() ? Commented May 1, 2013 at 8:43
  • You're not doing anything with the results of your query? You return them from your function, but don't keep them anywhere? Commented May 1, 2013 at 8:48
  • The problem is that he want to access a private function and this is not authorized by visibility in PHP. He has to do it in a public function or if it's extends, with a protected function. Private is always gonna return fatal error except if it's use inside the class. Commented May 1, 2013 at 8:50
  • @Simon that's his first problem, yes ;-) Commented May 1, 2013 at 8:50
  • No, that is not the problem Simon and Phil. No I cannot make it public. No, I am not trying to access the private functions outside the class. I am trying to call private functions within a public function inside the same class. How is that not clear? So Phil, how am I not keeping them anywhere? I am returning inside the private function, what do you mean I am not keeping them anywhere? Commented May 1, 2013 at 15:21

3 Answers 3

6

I will help you to analyse your code, section by section. The first part looks like this...

$members = new Club();
$members->Get_Members_From_DB();

Get_Members_From_DB() returns a value, but you do not store this value anywhere. Moreover, you're already within the Club class so we don't need a new Club. Instead try this...

$members = $this->Get_Members_From_DB();

Next up, you have...

while($row1 = $members)

But you're just assigning (copying) $members to $row1 here. And as long as $members is truthy this will loop forever. Instead, consider this...

foreach ($members as $member)
{
    // Do something with $member
}

And finally, you have a similar problem getting your interest results. Your code is...

$results2 = new Club;
$results2->Get_Members_Interests_From_DB($row1['Email']);

while($row2 = mysqli_fetch_array($results2))
{
    $interests[] = $row2;
    echo("<li>" . $row2['InterestDescription'] . "</li>");
}

Again, Get_Members_Interests_From_DB() is returning something, but you're not storing the value. How about this instead...

$interests = $this->Get_Members_Interests_From_DB($member['Email']);
foreach ($interests as $interest)
{
    echo '<li>', $interest['InterestDescription'], '</li>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Phil. You are a champ!!
$myClub = new Club("localhost","A340User","Pass123Word","info_club"); $date = date("Y/m/d"); $sql="INSERT INTO member (FirstName,LastName,Gender,Email,MemberSince) VALUES ('$fname','$lname','$gender','$email','$date');"; $result = mysqli_query($this->Con,$sql);
Would that work on the code I just posted? It's another function within the class. EDIT: Actually nevermind. That one works right now, I am not going to try to fix it.
2

In php7 the anonymous classes were introduced. All you need is to create a class extending the class with private function. Then you can add any functions to it and event create an accessor for the private functions. You would also need to create a converter to convert your object of the old class into an object of the new class and that is it.

That is all you need.

You could do it without php7, but with php7 you can avoid creating class definition somewhere else. With php7 you can do all conversion in one function or in one class method. Isn't it cool?

Comments

1

You cannot access 'private' functions outside of the class they're defined in. As Simon has commented, you need to make these public.

You should read about visibility in the PHP Manual, it will teach you about the differences between public, protected and private functions. Here's a short example:

class Example {

    public function aPublicMethod()
    {
        return 'public';
    }

    private function aPrivateMethod()
    {
        return 'private';
    }

}

$example = new Example;
// $aPublicString will become 'public'
$aPublicString = $example->aPublicMethod();
// error, you cannot see private functions outside of a class
$aPrivateString = $example->aPrivateMethod();

3 Comments

You are not understanding what I am saying then. I am trying to call them from within the class!!! I am trying to call them in a public function within the class. The function also has to stay private, I cannot change it to public. And I do not need to read the visibility manual. I already have.
Our apologies, it was not clear to me from your pasted code what's in what classes.
It's ok. I just have been working on this for three days and cannot understand how to get this to work with no parameters for the private functions, only being able to use a return statement, and I have read the visibility manual and the return information several times. I just don't get it.

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.