Jump to content

Class/Function Help


jmfillman

Recommended Posts

This code returns exactly what I would expect:

 

$query  = "SELECT year, subject, location FROM appointments"; $result = mysql_query($query);

 

while($row = mysql_fetch_assoc($result)) {

    echo "Year :{$row['year']} <br>" .

        "Subject : {$row['subject']} <br>" .

        "Location : {$row['location']} <br><br>"; }

 

However, I need it to be in a Class and Function. So when I do this:

class AppointmentsService {

public function getAppointments() {

$query  = "SELECT subject FROM appointments";

$result = mysql_query($query);

 

while($row = mysql_fetch_assoc($result)) {

    $appointments_array[] = $row; }

}

return ($appointments_array);

}

}

 

 

I get the following error:

Parse error: syntax error, unexpected T_RETURN, expecting T_FUNCTION in C:\wamp\www\Services\AppointmentsService.php on line 19

 

What is the problem?

 

Link to comment
https://forums.phpfreaks.com/topic/92612-classfunction-help/
Share on other sites

Try this:

 

class AppointmentsService {
   public function getAppointments() {
      $query  = "SELECT subject FROM appointments";
      $result = mysql_query($query);
      $appointments_array = array();

      while($row = mysql_fetch_assoc($result)) {
          $appointments_array [] = $row;
      }
      return $appointments_array;
   }
}

Link to comment
https://forums.phpfreaks.com/topic/92612-classfunction-help/#findComment-474653
Share on other sites

Okay, that got rid of the error, thank you  ;D  However, the array seems to be empty, but it should have 1 record in it. So I tried to output the array contents like this, but I don't get any errors and no output either.  ??? The following code just outpute NULL.

 

class AppointmentsService {

  public function getAppointments() {

      $query  = "SELECT subject FROM appointments";

      $result = mysql_query($query);

      $appointments_array = array();

 

      while($row = mysql_fetch_assoc($result)) {

          $appointments_array [] = $row;

      }

 

      return $appointments_array;

  }

 

}

echo 'var_dump($appointments_array);'."\n";

var_dump($appointments_array);

 

$myTest = new AppointmentsService;

$myTest->getAppointments();

Link to comment
https://forums.phpfreaks.com/topic/92612-classfunction-help/#findComment-474692
Share on other sites

You're not using the function properly, this is what you want:

 

<?php
class AppointmentsService {
   public function getAppointments() {
      $query  = "SELECT subject FROM appointments";
      $result = mysql_query($query);
      $appointments_array = array();

      while($row = mysql_fetch_assoc($result)) {
          $appointments_array [] = $row;
      }

      return $appointments_array;
   }

}
$myTest = new AppointmentsService;
$appointments = $myTest->getAppointments();
print_r($appointments);
?>

Link to comment
https://forums.phpfreaks.com/topic/92612-classfunction-help/#findComment-474701
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.