0

So I am creating a cronJob that will select ALL the users from my user table and then store the users full names in a variable. All that happens inside a while loop, inside the same loop I am selecting EVERYTHING from my customerLeads tables where the assignedTo column is equal to the users full name. Then inside this loop I want to record the customerName and store them all inside an array. So each user will have it's own array which has all the customersNames inside.

The purpose of this is to run this every morning so the users will get an email if they haven't updated a customerLead in over 2 days.

However I keep getting this error;

Fatal error: Uncaught Error: Call to a member function fetch() on boolean in /.../customerLeadReminder.php:18 Stack trace: #0 {main} thrown in /homepages/.../customerLeadReminder.php on line 18

I've had a look around online and everything says that it's the connection not working, but I've checked and the connection is running fine...

Question: Why does this error appear and what I am doing wrong?

<?php  
//Error Reporting
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);


require '../includes/conn.php';

  $userList = $salesConn->query("SELECT `email`, `firstname`, `lastname` FROM `users`");

  while ($uRow = $userList->fetch()) {

    $user_name = $uRow['firstname']." ".$uRow['lastname'];
    print_r($uRow);
    $customerList = $salesConn->query("SELECT * FROM `customerLeads` WHERE curdate() >= (dateUpdated + interval 2 day)  AND `assisgnedTo` = '$user_name' ORDER BY `customerID` DESC");
// show this on error
if (!$customerList) {
     // For PDO:
    echo $salesConn->errorInfo();
}
      while ($cRow = $customerList->fetch()) {
        $leadID = $cRow['customerID'];
        $firstName = $cRow['customerFirstName'];
        $lastName = $cRow['customerLastName'];
        $tele = $cRow['customerTel'];
        ....
        $dateCreated = $cRow['dateCreated'];
        $dateUpdated = $cRow['dateUpdated'];

      }
  }
  ?>

By printing $uRow it shows:

Array ( [email] => [email protected] [0] => [email protected] [firstname] => Joe [1] => Blogs [lastname] => Blogs [2] => Blogs )

Connection Page is:

<?php
$salesConn = new PDO('mysql:host=HOST;dbname=DBNAME', 'USERNAME', 'PASSWORD');
$salesConn->setAttribute(PDO::ATTR_ERRMODE);
?>

New Error: Warning: PDO::setAttribute() expects exactly 2 parameters, 1 given in /homepages/38/d735513801/htdocs/includes/conn.php on line 8

15
  • If i var_dump($salesConn); i get: object(PDO)#1 (0) { } Commented Feb 6, 2019 at 13:37
  • Sorry, Line 18 is: while ($cRow = $customerList->fetch()) { Commented Feb 6, 2019 at 13:40
  • 1
    Then please edit your question and below the current info. show us what your conn.php looks like. Cheers Commented Feb 6, 2019 at 14:08
  • 1
    This is where i get insulted...that is my conn.php there is nothing else...I know it's poor coding but I want to get it all working first... Commented Feb 6, 2019 at 14:50
  • 1
    I've replaced the data with DBNAME and PASSWORD for obvious reasons but apart from that, this is my conn.php page Commented Feb 6, 2019 at 15:03

3 Answers 3

1
SELECT * FROM `customerLeads` WHERE curdate() >= (dateUpdated + interval 2 day)  AND `assisgnedTo` = '$user_name' ORDER BY `customerID` DESC

You used two times WHERE clause. You had a syntax error in your mysql. And also better use parentheses in your queries when you want to compare the result of a number calculation.

Sign up to request clarification or add additional context in comments.

5 Comments

Nope, well I mean yes i did but that didn't make any difference to the error
Are you sure you are connected to the db then? Is your connection working? cause the above line should throw a straight forward syntax error.
I thought so too, but I'm 100% connected. I just did echo $user_name just before the while loop and it echo'd 1 username (should be 11) but it's definitely connected
Seems like a boolean value to me. $username is a concatenation of first and last name. So it should be something bigger than 1. Try to print_r($uRow); and see if you get any results there
Yeah, this was my result: Array ( [email] => [email protected] [0] => [email protected] [firstname] => Joe [1] => Blogs [lastname] => Blogs [2] => Blogs )
1

Try this to get a proper error message from MySQL

$customerList = $salesConn->query("SELECT * FROM `customerLeads` WHERE curdate() >= dateUpdated + interval 2 day AND WHERE `assisgnedTo` = '$user_name' ORDER BY `customerID` DESC");

// show this on error
if (!$customerList) {
     /***
      * NOTE: in a perfect world this should be:
      * error_log(print_r($salesConn->errorInfo(),true)); OR
      * error_log(print_r($salesConn->error,true));
      ***/

     // For MySQLi:
     echo $salesConn->error;

     // For PDO:
     echo $salesConn->errorInfo();
}

Comments

0

This is a tester script to establish what is wrong with your SQL.

localhost, DBNAME, USERNAME, PASSWORD are hardcoded values that the OP has not given and so the OP needs to update these themself.

This script below uses proper PDO and Exceptions. Get used to using Exceptions. Read about them, Learn them. This script also properly uses Prepared Statements - You really really (really) should be using Prepared Statements in your SQL.

<?php
error_log( 'php version: ', phpversion());

try {
    $salesConn = new PDO('mysql:host=localhost;dbname=*DBNAME*;charset=utf8', '*USERNAME*', '*PASSWORD*');  
    error_log( 'client version: ', $salesConn->getAttribute(PDO::ATTR_CLIENT_VERSION));
    error_log( 'server version: ', $salesConn->getAttribute(PDO::ATTR_SERVER_VERSION));
    $salesConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $salesConn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $err) {
    error_log(print_r($err->getMessage(),true));
    die('Error log ONE was generated.');
}

$sql = "SELECT * FROM `customerLeads` WHERE CURDATE() >= (dateUpdated + INTERVAL 2 DAY)  AND `assisgnedTo` = :assigned ORDER BY `customerID` DESC"

$user_name = "Set ths value to whatever the username is you want to check";

try
{
    $stmt = $salesConn->prepare($sql);
    $stmt->bindValue(':assigned', $user_name, PDO::PARAM_STR);
    $stmt->execute();
    // The below result can be put into a loop to output each $row in turn.
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $err)
{
    error_log(print_r($err->getMessage(),true));
    error_log(print_r($salesConn->errorInfo(),true));
    die('Error log TWO was generated.');
}

echo 'done. Got this far, everything worked!';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.