运行我的php文件从我的mysql服务器获取数据时出错

时间:2015-08-12 04:53:52

标签: php mysql pdo

  

警告:在第13行的C:\ xampp \ htdocs \ test.php中除以零

     

致命错误:在第16行的C:\ xampp \ htdocs \ test.php中调用boolean上的成员函数setFetchMode()

这是我的SQL查询

$sql = 'SELECT BeginTime, ChargedDuration, AreaName, AccountID
        FROM cdr
        WHERE BeginTime LIKE '%2015-07-20%'
        GROUP BY AreaName';

$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe)

2 个答案:

答案 0 :(得分:-1)

  

在布尔值

上调用成员函数setFetchMode()

如果内部有单引号,则使用$sql = "query"双引号。

try {
  $sql = "SELECT BeginTime, ChargedDuration, AreaName, AccountID
    FROM cdr
    WHERE BeginTime LIKE '%2015-07-20%'
    GROUP BY AreaName";
  $q = $conn->query($sql);
  $result = $q->setFetchMode(PDO::FETCH_ASSOC);
   while ($row = $q->fetch()) {
     echo $row['ID'] . "\t" . $row['name'] . "\t" . $row['whatever'] . "\n";
   }
}
  ....
}
catch (PDOException $e) {
  print $e->getMessage();
}

答案 1 :(得分:-1)

单引号问题...请在查询内单引号或双引号代替

此处更新了代码

$sql = 'SELECT BeginTime, ChargedDuration, AreaName, AccountID
        FROM cdr
        WHERE BeginTime LIKE $sql = \'%2015-07-20%\'
        GROUP BY AreaName';

$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe)