Mysql, PHP class functions help!

I’m trying to loop database fields into a function/method that is within a class which eventually will output the contents of the function, but the function is not outputting anything. However, when I manually assign the variables of the function, i.e. $variable1=‘username’; $variable2=‘username’… it outputs as it should.

Here’s the code:

class.php file

class Someclass{

public function somefunction($variable1, $variable2,$variable3){

       *Do stuff*


main.php file

$object = new Someclass();

$hostdb = ‘localhost’;
$namedb = ‘database’;
$userdb = ‘username’;
$passdb = ‘password’;

try {
// Connect and create the PDO object
$conn = new PDO(“mysql:host=$hostdb; dbname=$namedb”, $userdb, $passdb);
$conn->exec(“SET CHARACTER SET utf8”); // Sets encoding UTF-8

// Define and perform the SQL SELECT query
$sql = “SELECT * FROM table”;
$result = $conn->query($sql);

// Parse the result set
foreach($result as $row) {
$object->somefunction($row['username1'], $row['username2'],$row['username3']);

$conn = null; // Disconnect
catch(PDOException $e) {
echo $e->getMessage();

The only way this script outputs anything is if I do this:


I’ve also tried a while loop in the main.php file and loops within the class in the class.php file with no success. I echoed the database fields and echoed the function with the database fields in it and everything was in the database fields that should be so I don’t think its the database.

I am somewhat new to PHP, so can anybody help me out and let me know whats wrong, what I’m not doing or lend me some suggestions?


Verify there is data being retrieved from your database.

=== snip ===
// Parse the result set
foreach($result as $row) {
     //$object->somefunction($row['username1'], $row['username2'],$row['username3']);

Your code ASSUMES the each row of the table contains fields named “username1”, “username2”, “username3”.
Not that each “username” is from a separate row (user) in the table.