1

This is exactly what I need:

<?php
$conn = mysqli_connect($server, $buser, $pwd, $db);

$query = $conn->query("mysql_query here");

if (!query) {
    $error = $conn->error;
    $log_error = $conn->query("INSERT INTO tab (log) VALUES ('$error')");
}
?>

However, this does not work, the error is not being submitted into the db.

For how much it actually is bad to use a database to log errors, seems the only way to accomplish my needs.

What is needed: I need to log all errors from queries, no matter of the type, and log them somewhere which can be viewed on a private webpage accessible just for a specified level account with PHP just to display all those errors.

I already have everything working, account levels etc, just it seems can't find a way to log past occured errors on a php page in the following format:

account_who_caused_error || page_where_error_occured.php || error_message || datetime

error_log cannot be used due to the nature of what i need. so what exactly remains to do that?

5
  • What type of object is $conn? Commented Feb 15, 2017 at 15:44
  • $conn is a simple connection to database: $conn = mysqli_connect($webserver, $webuser, $webpwd, $webdb); Commented Feb 15, 2017 at 15:47
  • It seems a bad idea to log database errors in the database :-) If you need to view the log via a webpage you can still just log the errors in a text file and make that file accessible, or you could store them in a json file or CSV if the data structures are more complex. Commented Feb 15, 2017 at 15:52
  • have you tried escaping the $error variable? $log_error = $conn->query("INSERT INTO tab (log) VALUES ('" . mysqli_real_escape_string($error) . "')"); Commented Feb 15, 2017 at 15:54
  • 1
    Learn about prepared statements for MySQLi. Commented Feb 15, 2017 at 16:16

1 Answer 1

1

It is quite possible that the $error variable contains a single quote and thus is causing the query to be malformed. Escaping the $error variable may help:

$conn = mysqli_connect($server, $buser, $pwd, $db);

$query = $conn->query("mysql_query here");

if (!query) {
    $error = $conn->error;
    $log_error = $conn->query("INSERT INTO tab (log) VALUES ('" . mysqli_real_escape_string ($error) . "')");
}

As has been mentioned in the comments above, logging database errors in a database could be self-defeating. If your database has problems, no errors will be logged.

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

2 Comments

in such case, if you know, how i can save the error logs in a file and then make it accessible them on a php page, but, formatted in a table for 1 error line = 1 row, and datas formatted in columns? like error type, message, datetime and so on? Is it possible?
You could use fputcsv which will output into a CSV formatted file. That could then be read again using fgetcsv. See php.net/manual/en/function.fputcsv.php

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.