douoyou3348 2016-02-15 16:46
浏览 38
已采纳

Cron Job循环从mysqli对象中获取信息但print_r表示存在行; 在浏览器中运行时效果很好

I have a cron job that runs a few mysqli queries and then stores the info in an array for later use. When the cron job runs, the mysqli queries return no syntax errors. I did a print_r of the result and it shows that there were multiple rows in the mysqli object but when the foreach loop ran, no info was extracted from the result object. The funny part? I run the same script in the browser to debug and everything works perfectly. Below is the code. (Browser PHP version 5.7, server/cron PHP version 5.3)

$mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);

$emailContent = array();
$result = $mysqli->query("SELECT * FROM emailType ORDER BY emailNumber");
print_r($result);
foreach($result as $row) {
    print_r($row);
    $emailNumber = $row['emailNumber'];
    $email = array(
        "emailName" => $row['emailName'],
        "emailSlug" => $row['emailSlug'],
        "emailDescription" => $row['emailDescription'],
        "link" => $row['link'],
        "subject" => $row['subject'],
        "content1" => $row['content1'],
        "content2" => $row['content2']
    );
    $emailContent[$emailNumber] = $email;
}

I included the return of the print_r methods from above. When running the cron, the print_r($result) returns this:

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

While the print_r($row) returns nothing. When running this through a browser I get the same info print_r($result) but I also get a mysql record for print_r($row). It is important to note that I get no error at all.

  • 写回答

1条回答 默认 最新

  • douyue7408 2016-02-15 17:01
    关注

    Extending my comments to the question here is a simple example to demonstrate how to correctly use that object you get returned, when running in query:

    <?php
    // ...
    $result = $mysqli->query("SELECT * FROM emailType ORDER BY emailNumber");
    print_r($result);
    while($row = $result->fetch_assoc()) {
        print_r($row);
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行