jmfillman Posted February 23, 2008 Share Posted February 23, 2008 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? Quote Link to comment Share on other sites More sharing options...
p2grace Posted February 23, 2008 Share Posted February 23, 2008 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; } } Quote Link to comment Share on other sites More sharing options...
jmfillman Posted February 23, 2008 Author Share Posted February 23, 2008 Okay, that got rid of the error, thank you 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(); Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted February 23, 2008 Share Posted February 23, 2008 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); ?> Quote Link to comment Share on other sites More sharing options...
jmfillman Posted February 23, 2008 Author Share Posted February 23, 2008 Awsome, thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.