23

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));

?>
8
  • I doubt PHP has permissions to write to /. You should pick a better log file location. Maybe __DIR__ . '/errorlog.log' Commented Oct 21, 2013 at 5:03
  • what does __ DIR __ do and my file is 777 Commented Oct 21, 2013 at 5:04
  • google.com.au/search?q=php+__DIR__. I highly doubt your error log is sitting in the root directory Commented Oct 21, 2013 at 5:04
  • i don't know if you get alerted of comment edits so i will say it again i have 777 on the file it should have rights Commented Oct 21, 2013 at 5:06
  • You're telling me you have an errorlog.log file sitting in the filesystem root (along with /home, /etc) and it has the permission mask 0777? Commented Oct 21, 2013 at 5:08

1 Answer 1

56

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If there's a boolean as a value or an empty string it won't show up. This can make it difficult to determine data type etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.