Jayreis
Jayreis

Reputation: 287

PHPExcel Fatal error: Call to a member function put() on a non-object

I have recently moved my website from a shared hosting account to a VPS which I mention because I think it may have something to do with it. Anyway I am using PHPExcel https://phpexcel.codeplex.com to generate an excel sheet with content pulled from mysql table. The script worked fine on the shared hosting account but now all of a sudden is not working and instead displaying a error that says Fatal error: Call to a member function put() on a non-object the file and row number it is referencing looks like the below

$user->put($dateProcessed, $amount);


   //below is the code for the page

/**
 * @author Gary Drocella
 * @date 01/10/2014
 * Description: Performs data mining and generates a spread sheet from database.
 */

    $todaydate = date('Y-m-d');

require_once '/home/***/public_html/reports/User.php';
require_once '/home/****/public_html/reports/SpreadsheetGenerator.php';

$dbh = Connection::getConnection();
mysql_select_db("*****", $dbh);


$resultSet = mysql_query("SELECT sys_userprofiles.uid, sys_userprofiles.f_name, 
                              sys_userprofiles.l_name,  sys_userprofiles.acctstatus_id,
                              sys_userlogins.acct_status 
                              FROM sys_userprofiles, sys_userlogins WHERE 
                              sys_userprofiles.acctstatus_id NOT IN(0, 11, 23, 26, 28, 
                              30) AND sys_userprofiles.uid = sys_userlogins.uid AND 
                              sys_userlogins.acct_status = 'active' AND  
                              sys_userlogins.u_level = 2", $dbh);
$userTable = array();

/* Create a user Table */

while($row =mysql_fetch_row($resultSet)) {
    $uid = $row[0];
    $fName = $row[1];
    $lName = $row[2];

    $currUser = new User($uid, $fName, $lName);
    $userTable[$uid] = $currUser;

}

/* Populate user (date,amount) maps */

$resultSet = mysql_query("SELECT DISTINCT uid, str_to_date(paymentdate, '%Y-%m-%d') AS date_processed, SUM(amount) AS total
                          FROM sys_userpayschedule WHERE uid IN (SELECT uid FROM sys_userlogins WHERE acct_status = 'active' AND u_level = 2) 
                          AND str_to_date(paymentdate, '%Y-%m-%d') >= curdate() AND str_to_date(paymentdate, '%Y-%m-%d') <= DATE_ADD(curdate(), interval 6 MONTH) 
                          GROUP BY date_processed, uid ORDER BY date_processed", $dbh);

while($row=mysql_fetch_row($resultSet)) {
    $uid = $row[0];
    $dateProcessed = $row[1];
    $amount = $row[2];

    $user = $userTable[$uid];
    $user->put($dateProcessed, $amount);
}
$spreadSheetGen = new SpreadsheetGenerator();
$spreadSheetGen->generate($userTable);

Again I have changed nothing other then moving all files from a shared hosting account to a VPS. So I am wondering if maybe their is some sort of server side module I may need to install that is standard on a shared hosting account to make this work? I double checked that all files are in fact moved and in the correct place to match what was on the shared hosting account. I can post the full pages code if need be.

Any suggestions??

If I do a var_dump($user); from my second query I get an output that has the urserid's and names with the exception I think of one row or so Below is the var_dump of $user

object(User)#240 (5) { ["uid":"User":private]=> string(5) "10489" ["firstName":"User":private]=> string(5) "Helen" ["lastName":"User":private]=> string(12) "Throckmorton" ["dateTotalTable":"User":private]=> NULL ["dateTable"]=> array(0) { } } object(User)#258 (5) { ["uid":"User":private]=> string(5) "10516" ["firstName":"User":private]=> string(7) "Russell" ["lastName":"User":private]=> string(8) "Demedina" ["dateTotalTable":"User":private]=> NULL ["dateTable"]=> array(0) { } } object(User)#2269 (5) { ["uid":"User":private]=> string(5) "13201" ["firstName":"User":private]=> string(7) "Orlando" ["lastName":"User":private]=> string(14) "Lewis-Maryland" ["dateTotalTable":"User":private]=> NULL ["dateTable"]=> array(0) { } } object(User)#2270 (5) { ["uid":"User":private]=> string(5) "13203" ["firstName":"User":private]=> string(17) "Latrinda S" ["lastName":"User":private]=> string(5) "Clark" ["dateTotalTable":"User":private]=> NULL ["dateTable"]=> array(0) { } } NULL 

Upvotes: 0

Views: 3445

Answers (3)

Avik Aghajanyan
Avik Aghajanyan

Reputation: 1023

Your second query contains a uid, which does not contain in your first query. And so looking at your var_dump(): you have 4 users object and 1 NULL (the last one). And the error is thrown on that last record. I would suggest to change:

$user = $userTable[$uid];
$user->put($dateProcessed, $amount);

into this:

if ($user = $userTable[$uid]) {
  $user->put($dateProcessed, $amount);
}

Safer version will be:

if (isset($userTable[$uid]) && $user = $userTable[$uid]) {
  $user->put($dateProcessed, $amount);
}

...to not get invalid index warnings...

Upvotes: 0

Marc B
Marc B

Reputation: 360702

The error is very clear - you're trying to use something as an object, when it ISN'T an object:

$user = $userTable[$uid];
$user->put($dateProcessed, $amount);

That most likely means that whatever is in $uid is looking up an array element in $userTable that DOESN'T exist, so $user will be null. You then try to use that null as an object, and boom. there's your error.

Upvotes: 1

Evan
Evan

Reputation: 123

It seems, as if you have no user object there. Either you grab a dataset with the second query, that did not exist in the first query or you did not grab any user at all with your first query. Use var_dump($user) and print out the user ids in both while-loops. I think you will find the error then. I don't see any other problems.

Besides that: You should drop mysql_query and use mysqli or PDO. mysql_ is deprecated and will be removed.

An Addition to your latest edit: You already noticed, that one user is not included. It seems, that you select a user in your second query, that you have not selected in your first query. Let's analyse your queries a bit:

SELECT ... WHERE 
                          sys_userprofiles.acctstatus_id NOT IN(0, 11, 23, 26, 28, 
                          30) AND sys_userprofiles.uid = sys_userlogins.uid AND 
                          sys_userlogins.acct_status = 'active' AND  
                          sys_userlogins.u_level = 2

Here you select all users excluding the ones with the acctstatus_ids 0, 11, 23, 26, 28 and 30.

SELECT ... WHERE uid IN (SELECT uid FROM sys_userlogins WHERE acct_status = 'active' AND u_level = 2)...

Here (in the subquery) you select all users excluding no user ids. So it could be, that your second query returns more users than your first one. And this could be your problem. You populate your user query based on your first selection and then you want to add details based on your second selection. If the second query however returns more users than your first one, your code will try to add details to a non existing object. Thus you get the error Call to a member function put() on a non-object the file, because $user is not an object for this particular id.

Upvotes: 0

Related Questions