jeudi 8 octobre 2015

PHP scope issue within while loop

Trying to teach myself php and I am struggling with what I think is a scoping issue?

I have a query, while loop, if/else and a function.

I would like to echo the results of the function ($data) within my while loop, however I'm not sure how to do this.

My code so far is as follows (stripped for legibility);

$stmt = $conn->prepare("SELECT... ...)  //QUERY here ...

if( $stmt->num_rows > 0 )
{
    while ($stmt->fetch()) {
       if (SomethingIsTrue){
             echo $data; // from get_patronapi_data($id) below
            }
       else {
             echo $somethingElse;
            }  
    }//end while
}//end if

function get_patronapi_data($id) {
            // ...
            return $data; // I want to use this in the while loop above
            //...
        }

echo get_patronapi_data($id); // this works and echoes $data on screen

Whenever I echo get_patronapi_data($id); I successfully see the data on screen.

I'm sure I am doing something very silly however any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire