Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly print variable to PHP error log

Tags:

php

I'm trying to properly print a variable to the error log so that I know if my MySQL query worked, but it doesn't print anything to my errorlog.log file. I set everything up below, I set errors to true and told it the file to print to but it doesn't print anything at all:

<?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())
  {

I defined $results, here its a MySQL query that fetches a bunch of info from the database and returns it in $results:

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


echo json_encode($results);

This is where I try to print the variable to the error log file:

error_log(print_r($results));

?>
like image 469
user2354835 Avatar asked Oct 21 '13 04:10

user2354835


People also ask

How do I create a PHP error log?

Enable Error Logging in php. To log errors in PHP, open the php. ini file and uncomment/add the following lines of code. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How can I get error message in PHP?

The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); The ini_set function will try to override the configuration found in your php.

Where do I log PHP errors?

If the syslog is used, then all PHP errors will be sent directly to the default system log file—in Linux, this is typically /var/log/syslog. The more manageable method is to use a custom log file.

How can we log error messages to a file?

Approach 1: The error_log() function can be used to send error messages to a given file. First argument to the function is the error message to be sent. Second argument tells where to send/log the error message. In this case, second argument is set to 3, used to redirect error message to a file.


1 Answers

print_r (php manual) will print the array and won't return a value, so basically you're using it wrong.

The right way is using the second parameter of the function which is boolean that determines if the function will print the output or will return it.

error_log(print_r($results,true));

EDIT If your server has DirectAdmin or CPanel, there's a built-in option to see the Apache error logs. Check if your custom error appears there. If so, there's a problem with the errorlogs file.

like image 162
Ofir Baruch Avatar answered Oct 04 '22 04:10

Ofir Baruch