4

I was wondering if someone could help me with this problem. I want to print/echo the solution of the function multiple times on the same page. Is that possible?

Here's my function:

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return $rResult;
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

And this is how I manage to call on the function by now. But it only works once:

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }

I hope someone can help! Thanks anyway.

1
  • Are you sure your result has more than one row? Commented May 21, 2011 at 11:18

3 Answers 3

1

Create a function for your printing task, then call it as many times as you want:

function print_the_stuff($feedbackPatient){

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }
}

Then, wherever you need to print:

print_the_stuff($feedbackPatient);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot man! I was looking for this for about three days. Thanks!
So where does $allUsers come from? It's certainly not within the scope of this function. Also, one call from this will empty out the MySQL result set. It's not repeatable.
In the page where the result should come I do this: $allUsers = $oUser->getFeedback($_SESSION['id']);
1

The easiest way to do this is probably to get the whole result set in one go, and then pass that around.

This might be costly in terms of performance if the result set is large, but it's probably not going to make a significant difference. It is particularly easy with the mySqli extension, because you can use mysql_fetch_all.

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return mysqli_fetch_all($rResult, MYSQLI_ASSOC);
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

You would then get an associative array returned from getFeedback, and could loop through this as normal:

$feedback = getFeedback($id);
foreach ($feedback as $item) {
    echo $item['DiaryOpmerkingen'];
}

1 Comment

This gives me "Fatal error: Call to undefined function mysqli_fetch_all() in /Users/Kim/Sites/snooze/classes/User.class.php on line 350" .. any idea why?
0

I think it should be :

$feedbackPatient = getFeedback($p_iUserid);
    if(mysqli_num_rows($feedbackPatient) > 0)
                        {
                            while($oUser = mysqli_fetch_assoc($feedbackPatient))
                            {
                                echo $oUser['DiaryOpmerkingen'];
                            }
                        }

why are u using diff result for mysqli_num_rows and sqli_fetch_assoc ?

Comments

Your Answer

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