19

我正在尝试将变量正确打印到错误日志中,以便我知道我的 MySQL 查询是否有效,但它不会在我的errorlog.log文件中打印任何内容。我在下面设置了所有内容,我将错误设置为 true,并告诉它要打印到的文件,但它根本不打印任何内容:

<?php

error_reporting(E_ALL); //to set the level of errors to log, E_ALL sets all warning, info , error

ini_set("log_errors", true);
ini_set("error_log", "/errorlog.log"); //send error log to log file specified here.

include ("connection.php");

$city = $_POST['city'];

$state = $_POST['state'];

$results = array();
if( $query =  $db->query("SELECT business_id, BusinessName, date, post".
"FROM WolfeboroC.posts".
"JOIN WolfeboroC.users ON users.recid = posts.business_id".
"WHERE city= '$city' && state='$state' ".
"ORDER BY date DESC LIMIT 0, 500") ) 
{
  while($record = $query->fetch_assoc())
  {

我定义$results了,这里是一个 MySQL 查询,它从数据库中获取一堆信息并在 $results 中返回它:

    $results[] = $record;
  }
  $query->close();
}


echo json_encode($results);

这是我尝试将变量打印到错误日志文件的地方:

error_log(print_r($results));

?>
4

1 回答 1

47

print_r( php manual ) 将打印数组并且不会返回值,所以基本上你使用它是错误的。

正确的方法是使用函数的第二个参数,它是布尔值,用于确定函数是打印输出还是返回它。

error_log(print_r($results,true));

编辑 如果您的服务器有 DirectAdmin 或 CPanel,则有一个内置选项可以查看 Apache 错误日志。检查您的自定义错误是否出现在那里。如果是这样,则错误日志文件有问题。

于 2013-10-21T06:19:56.267 回答