PHP 多个数据库连接失败

标签 php mysql database while-loop fetch

因此,我正在创建一个 cronJob,它将从我的用户表中选择所有用户,然后将用户全名存储在变量中。所有这些都发生在 while 循环内,在同一个循环内,我从 customerLeads 表中选择所有内容,其中 signedTo 列等于用户全名。然后在这个循环中,我想记录 customerName 并将它们全部存储在一个数组中。因此每个用户都会有自己的数组,其中包含所有的customersNames。

这样做的目的是每天早上运行它,这样如果用户在超过 2 天内没有更新 customerLead,他们就会收到一封电子邮件。

但是我不断收到此错误;

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

我上网查了一下,一切都说是连接不工作,但我检查了一下,连接运行正常...

问题:为什么会出现此错误以及我做错了什么?

<?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] => Blogs )

连接页面是:

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

新错误:警告:PDO::setAttribute() 需要 2 个参数,其中 1 个在/homepages/38/d735513801/htdocs/includes/conn.php 第 8 行中给出

最佳答案

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

您使用了两次 WHERE 子句。你的 mysql 中有语法错误。当您想要比较数字计算的结果时,最好在查询中使用括号。

关于PHP 多个数据库连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54554773/

相关文章:

java - PHP 中的三层方法

php - 当我使用带有常量值的 bindParam 时,为什么出现 Cannot pass parameter 2 by reference 错误?

php - 如何在 ubuntu 机器上安装 oracle instantclient 和 pdo_oci?

javascript - 防止ajax的多个请求

php - 关注者/关注数据库结构

mysql - 基于 lower_case_table_names 系统变量的 information_schema.routines 上的 SELECT 行为

sql - 数据库可选参数错误

mysql - 如何使用 CodeIgniter 连接到 Google Cloud SQL 数据库?

MySQL语法错误

mysql - 我应该在 MySQL InnoDB 表的 id 列上应用 PRIMARY 和 UNIQUE 索引吗?