因此,我正在创建一个cronJob,它将从用户表中选择所有用户,然后将用户的全名存储在变量中。所有这些都发生在while循环内,并且在同一循环内,我从assignedTo
列等于用户全名的customerLeads表中选择EVERYTHING。然后在此循环中,我想记录customerName并将它们全部存储在数组中。因此,每个用户将拥有自己的数组,其中包含所有customersName。
此操作的目的是每天早上运行一次,这样,如果用户在两天内未更新customerLead,他们将收到一封电子邮件。
但是我仍然收到此错误;
致命错误:未捕获错误:在/.../customerLeadReminder.php:18中的布尔值上调用成员函数fetch():18堆栈跟踪:#0 {main}抛出在/homepages/.../customerLeadReminder.php中在第18行
我在网上浏览了一下,一切都说这是连接不正常,但是我检查了一下,连接运行正常...
问题:为什么会出现此错误,我在做什么错了?
<?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'];
}
}
?>
通过打印$uRow
,它显示:
Array([email] => joe.blogs@outlook.com [0] => joe.blogs@outlook.com [firstname] => Joe [1] => Blogs [lastname] => Blogs [2] =>博客)
连接页面为:
<?php
$salesConn = new PDO('mysql:host=HOST;dbname=DBNAME', 'USERNAME', 'PASSWORD');
$salesConn->setAttribute(PDO::ATTR_ERRMODE);
?>
新错误:警告:PDO :: setAttribute()恰好需要2个参数,第8行的/homepages/38/d735513801/htdocs/includes/conn.php中给出了1个参数
答案 0 :(得分:1)
尝试从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();
}
答案 1 :(得分:1)
SELECT * FROM `customerLeads` WHERE curdate() >= (dateUpdated + interval 2 day) AND `assisgnedTo` = '$user_name' ORDER BY `customerID` DESC
您使用了两次WHERE子句。您的mysql中有语法错误。当您要比较数字计算的结果时,最好在查询中使用括号。
答案 2 :(得分:0)
localhost , DBNAME , USERNAME , PASSWORD 是OP尚未提供的硬编码值,因此OP需要自己更新这些。
下面的脚本使用正确的PDO和异常。习惯使用异常。 Read about them, Learn them。此脚本还正确使用了Prepared Statements-您真的 really ( really )应该在SQL中使用Prepared Statements。
<?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!';