Gags
Gags

Reputation: 3839

Write Errors to LOG file - PHP mySql

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?

Upvotes: 0

Views: 2808

Answers (1)

tiomno
tiomno

Reputation: 2228

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!

Upvotes: 2

Related Questions