PHP:通过CLI运行cronjob时,MySQLi查询返回false并返回null错误,但如果通过Web服务器调用脚本则可以使用

时间:2018-11-01 01:07:12

标签: php mysqli cron

此脚本已经运行了好几年,在某些情况下仍然可以正常运行,但是我正在尝试验证另一种情况,但是它失败了,没有任何错误消息。

我整天都在为此而苦苦挣扎,而且已经走到了尽头。

我将以下内容与3个电子邮件收件箱一起使用-检查电子邮件,使用主题中的详细信息验证客户,然后设置客户对象,然后再向客户插入电子邮件内容。

对于已经存在的2个邮箱,这已经并且仍然可以正常工作,但是,第三个(新邮箱)只是在$this->mysqli->query($query)返回false时一直失败。

正如我通过输出电子邮件主题所看到的那样,它正在正确提取电子邮件-它具有,已经阅读,已经正确构建了SQL查询...只是无法成功执行...

        //Build the query
    $query = "SELECT id, 
                     parent_id, 
                     name, 
                     storeno,
                     make,
                     active_advisors,
                     address,
                     primary_contact,
                     primary_phone,
                     primary_email,
                     secondary_contact,
                     secondary_phone,
                     secondary_email,
                     daily_email_receipients,
                     wty_analysis_email_recipients,
                     created,
                     expiry,
                     modified,
                     modified_by,
                     active,
                     customer_type
              FROM Customer
              WHERE LOWER(name) = LOWER('".addslashes($emailHeader['customername'])."')
                AND LOWER(storeno) = LOWER('".$emailHeader['storeno']."')
                AND LOWER(make) = LOWER('".$emailHeader['make']."')";

        //debug query
        print $query;

        //Execute the query and validate that it was executed without issue
        if( $result = $this->mysqli->query($query) ) {

            //Print row count for debugging
            print "Select returned ".mysqli_num_rows($result)." matching rows for 'Customer' lookup.";

            //Check to see if we got what we expected (an single exact match!)
            if (mysqli_num_rows($result)==1) {

                //Transfer the result into a usable Object
                while ($row = $result->fetch_object()) {

                    $this->setCustomerInformation($row);
                }
            }

            // If we get 0 then we have no match
            // If we get > 1 then we have a problem (and still no match)
            else { 

                print "Could not match email details to a customer: (".implode(",",$emailHeader).")"; 
                return false;
            }

        }
        else { 
            print 'Mysqli Query returned false: " ('.serialize($this->mysqli).')';
            print mysqli_errno($this->mysqli);
            print mysqli_error($this->mysqli);
            return false;
        }   

输出:

SELECT id, 
                     parent_id, 
                     name, 
                     storeno,
                     make,
                     active_advisors,
                     address,
                     primary_contact,
                     primary_phone,
                     primary_email,
                     secondary_contact,
                     secondary_phone,
                     secondary_email,
                     daily_email_receipients,
                     wty_analysis_email_recipients,
                     created,
                     expiry,
                     modified,
                     modified_by,
                     active,
                     customer_type
              FROM Customer
              WHERE LOWER(name) = LOWER('XXXXXXXX')
                AND LOWER(storeno) = LOWER('S01')
                AND LOWER(make) = LOWER('FO')

Mysqli Query returned false: " (O:6:"mysqli":19:{s:13:"affected_rows";N;s:11:"client_info";N;s:14:"client_version";N;s:13:"connect_errno";N;s:13:"connect_error";N;s:5:"errno";N;s:5:"error";N;s:10:"error_list";N;s:11:"field_count";N;s:9:"host_info";N;s:4:"info";N;s:9:"insert_id";N;s:11:"server_info";N;s:14:"server_version";N;s:4:"stat";N;s:8:"sqlstate";N;s:16:"protocol_version";N;s:9:"thread_id";N;s:13:"warning_count";N;})

到目前为止我所做的:

  1. 我已输出$ query并在MySQL中运行,它成功返回了正确的结果
  2. 我在所有3个邮箱检查中都比较了mysqli对象,它们是相同的
  3. 我已经在每个收件箱中运行了一封邮件(前两个=成功),第三个失败
  4. 我只用第三个邮箱中的一封邮件运行该进程=失败(认为我正在重置连接)

我的想法已经用完了-测试并且不知道去哪里

错误输出为NULL

其他测试-该过程正在通过cron作业运行-我刚刚通过浏览器手动运行了该过程,并且可以正常运行...

  • 浏览器中的PHP版本与cli相同
  • Mysqli对象在浏览器和CLI中是相同的
  • mysqli-> Query()结果不同:

CLI:

NULL

浏览器:

mysqli_result Object
(
    [current_field] => 
    [field_count] => 
    [lengths] => 
    [num_rows] => 
    [type] => 
)

刚刚发现的mysqli-> stat()

CLI:NULL 浏览器:已填充

很明显MySQLi对象有问题

1 个答案:

答案 0 :(得分:-1)

在查询数据之前尝试致电mysqli_report(MYSQLI_REPORT_ALL)。它启用或禁用内部报告功能。

mysqli_report on php.net

我也建议不要混用PHP: MySQLi库的面向对象和过程样式。