PHP MySQL在phpmyadmin和脚本中运行时返回不同的值

时间:2015-07-20 21:17:49

标签: php mysql phpmyadmin

我有两个函数从我们的数据库中提取product_ids。 “topSellers”函数SQL在phpmyadmin中返回预期的结果,但是当从脚本运行时,它有一个意外的(对我而言)输出。

function topSellers(){
    $db = \Skeet\DatabaseFactory::getDatabase(FRAMEWORK_DB);
    $sql = "SELECT product_id, COUNT(*) AS totalCount FROM order_line WHERE creation_datetime > '" .  date("Y-m-d",strtotime("-30 days")) . "' AND is_retired = 0 GROUP BY product_id ORDER BY totalCount DESC LIMIT 20";
    echo $sql;
    $rows = $db->doQuery($sql)->getResultAsArray();      
    var_dump($rows);    
    return $rows;
}
function newProducts(){ // Works great
    $db = \Skeet\DatabaseFactory::getDatabase(FRAMEWORK_DB);
    $sql = "SELECT product_id FROM product WHERE creation_datetime > '" .  date('Y-m-d',strtotime('-90 days')) . "' AND product_name NOT LIKE '%Build_Bundled_Package%' AND price > 0 AND is_retired = 0 ORDER BY creation_datetime DESC LIMIT 20";   
    $rows = $db->doQuery($sql)->getResultAsArray();   

    return $rows;
}

打印的$ sql是:

SELECT product_id, COUNT(*) AS totalCount FROM order_line WHERE creation_datetime > '2015-06-20' AND is_retired = 0 GROUP BY product_id ORDER BY totalCount DESC LIMIT 20

在phpmyadmin中,它返回排序计数的两列,并按20行排序。

从站点运行时,它返回一个空数组。或者,如果我尝试一个对象:

object(Skeet\Database\MysqliDatabaseResult)#932 (1) {
  ["result":protected]=>
  object(mysqli_result)#98 (5) {
    ["current_field"]=>
    int(0)
    ["field_count"]=>
    int(2)
    ["lengths"]=>
    NULL
    ["num_rows"]=>
    int(0)
    ["type"]=>
    int(0)
  }
}

这结束了工作:

function topSellers(){
   $date = date('Y-m-d', strtotime("-30 days"));        

  try {  
    $conn = new PDO('mysql:host=localhost;dbname=******', DB_USERNAME, DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT product_id, count(quantity) AS totalCount FROM order_line WHERE creation_datetime > :date GROUP BY product_id ORDER BY totalCount DESC');
    $stmt->execute(array(':date' => $date));    

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }



return $result;
}

0 个答案:

没有答案