0

I am using mysqli to handle Database operations and want to Log each and every error to a file sql-error.log

Code i am using is below:

$result = mysqli_query($this->_con,"CALL PROCEDURE_NAME(2)");
if(false===$result){
  $error = 'Error calling stored procedure insert_product. Error No: ' . 
            mysqli_errno($this->_con) . ': ' . mysqli_error($this->_con);
  error_log($error . "Date::" . date("l jS \of F, Y, h:i:s A") ."\n", 3, "/sql-errors.log");
  exit();
}

And on cPanel, my Functions.php folder structure is as below:

/public_html/site_name/sql/functions/

But when i execute the query then i got a below Error

[15-Jan-2016 04:21:06 Etc/GMT] PHP Warning: error_log(/sql-errors.log): failed to open stream: Permission denied in /public_html/site_name/sql/functions/Functions.php on line ***

How to give permissions to write to this file and will error_log automatically creates file?

6
  • Add permission to a folder where error_log is located Commented Jan 15, 2016 at 5:31
  • like, i shall change from 755 to 777 . Is it safe to do so?? Commented Jan 15, 2016 at 5:31
  • Yes it will be safe, you need to change 755 to 777 Commented Jan 15, 2016 at 5:35
  • 600 is safe : The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private. Commented Jan 15, 2016 at 5:35
  • changing Permission to 0777 has not worked and is still a problem Commented Jan 15, 2016 at 5:37

1 Answer 1

2

I think PHP is trying to write on the root folder which it doesn't have permission to access to. So, I'd rather use the __DIR__ constant to access the desired folder where the log files reside.

Let's say you have a log folder /public_html/site_name/log/. Since your script is in /public_html/site_name/sql/functions/ what I'd program is:

error_log( $error . "Date::" . date( "l jS \of F, Y, h:i:s A" ) . "\n", 3, _DIR_ . "/../../log/sql-errors.log" );

Hope it helps!

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

1 Comment

Brilliant @timo ... Worked like Charm :) and that too without changing Permisions.

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.