【问题标题】:PHP Multiple DB Connections FailingPHP多个数据库连接失败
【发布时间】:2019-06-30 11:22:30
【问题描述】:

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

这样做的目的是每天早上运行,以便用户在超过 2 天没有更新 customerLead 时会收到一封电子邮件。

但是我不断收到此错误;

致命错误:未捕获的错误:调用 /.../customerLeadReminder.php:18 中布尔值的成员函数 fetch() 堆栈跟踪:在 /homepages/.../customerLeadReminder.php 中抛出 #0 {main}在第 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 它显示:

数组 ( [电子邮件] => joe.blogs@outlook.com [0] => joe.blogs@outlook.com [名字] => 乔 [1] => 博客 [姓] => 博客 [2] => 博客)

连接页面是:

<?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 行给出

【问题讨论】:

  • 如果我 var_dump($salesConn);我得到:对象(PDO)#1(0){}
  • 对不起,第 18 行是:while ($cRow = $customerList->fetch()) {
  • 那么请在当前信息下方编辑您的问题。向我们展示您的conn.php 的样子。干杯
  • 这是我被侮辱的地方...那是我的 conn.php 没有别的...我知道这是糟糕的编码,但我想先让它全部工作...
  • 出于显而易见的原因,我已经用 DBNAME 和 PASSWORD 替换了数据,但除此之外,这是我的 conn.php 页面

标签: php mysql database while-loop fetch


【解决方案1】:
SELECT * FROM `customerLeads` WHERE curdate() >= (dateUpdated + interval 2 day)  AND `assisgnedTo` = '$user_name' ORDER BY `customerID` DESC

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

【讨论】:

  • 不,我的意思是我做了,但这对错误没有任何影响
  • 你确定你已经连接到数据库了吗?你的连接正常吗?导致上面的行应该抛出一个直接的语法错误。
  • 我也是这么想的,但我是 100% 连接的。我只是在 while 循环之前回显了 $user_name,它回显了 1 个用户名(应该是 11),但它肯定是连接的
  • 对我来说似乎是一个布尔值。 $username 是名字和姓氏的串联。所以它应该大于 1。尝试print_r($uRow); 看看是否有任何结果
  • 是的,这是我的结果:Array ( [email] => joe.blogs@outlook.com [0] => joe.blogs@outlook.com [firstname] => Joe [1] => 博客 [姓] => 博客 [2] => 博客 )
【解决方案2】:

尝试从 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();
}

【讨论】:

    【解决方案3】:

    这是一个测试脚本,用于确定您的 SQL 出了什么问题。

    localhostDBNAMEUSERNAMEPASSWORD 是 OP 没有给出的硬编码值,因此OP 需要自己更新这些。

    下面的这个脚本使用了正确的 PDO 和异常。习惯使用异常。 Read about them, Learn them。该脚本还正确使用了Prepared Statements - 你真的真的真的)应该在你的 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!';
    

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      相关资源
      最近更新 更多