<?php
error_reporting(E_ALL);
ini_set('log_errors', TRUE);
ini_set('error_log', 'error_log');
ini_set('display_errors', TRUE);
<?php
error_reporting(E_ALL);
ini_set('log_errors', TRUE);
ini_set('error_log', 'error_log');
ini_set('display_errors', FALSE);
<?php
@mysql_query( ... );
In my experience, there is almost never any justification for using this. So just don't! Instead, write a bit more code to test for the error conditions you expect. And avoid those error conditions.
Error suppression should be avoided if possible as it doesn't just suppress the error that you are trying to stop, but will also suppress errors that you didn't predict would ever occur. This will make debugging a nightmare.
<?php
$link = mysqli_connect($db_host, $db_user, $db_word, $db_name) or die('Cannot connect to database');
<?php
$result = mysqli_query($link, 'SELECT * FROM myTable') or die('Query failed');
<?php
$link = mysqli_connect($db_host, $db_user, $db_word, $db_name);
if (!$link)
{
$err
= 'MySQLi Error: '
. mysqli_connect_errno()
. ' '
. mysqli_connect_error()
;
trigger_error($err, E_USER_ERROR);
}
Armed with that information, you can make a Google search for MySQL Error: 1045 Access denied for user, and you will links to a variety of helpful resources!
Fatal error: MySQLi Error: 1045 Access denied for user ''@'localhost' (using password: NO) in /path/to/script.php on line ...
<?php
$link = mysqli_connect($db_host, $db_user, $db_word, $db_name);
if (!$link)
{
$msg
= 'MySQLI Error: '
. mysqli_connect_errno()
. ' '
. mysqli_connect_error()
;
$err
= '<a target="_blank" href="https://www.google.com/#q='
. urlencode($msg)
. '">'
. htmlentities($msg)
. '</a>'
;
trigger_error($err, E_USER_ERROR);
}
<?php
$sql = 'SELECT Error';
$result = mysqli_query($link, $sql);
if (!$result)
{
$msg
= 'MySQLI Error: '
. mysqli_errno($link)
. ' '
. mysqli_error($link)
;
$err
= '<a target="_blank" href="https://www.google.com/#q='
. urlencode($msg)
. '">'
. htmlentities($msg)
. '</a>'
. " SQL: $sql"
;
trigger_error($err, E_USER_ERROR);
}
<?php // /find_error_log.php
/**
* Put this script in the web root or other top-level directory
*
* Traverse this directory and all directories of the web tree
* Show and optionally delete the error log files
*
* http://php.net/manual/en/class.recursivedirectoryiterator.php#85805
*/
ob_start();
error_reporting( E_ALL );
ini_set( 'display_errors', TRUE );
ini_set( 'log_errors', TRUE );
// START IN THE CURRENT DIRECTORY
$path = realpath(getcwd());
$plen = strlen($path);
// THE ERROR LOG FILE NAME - REVERSED BECAUSE IT IS AT THE END OF THE PATH STRING
$error_log = ini_get('error_log');
$error_log = basename($error_log);
$error_log = strrev($error_log);
// IF THERE IS A POST-METHOD REQUEST TO DELETE THIS ERROR LOG
if (!empty($_POST['error_log']))
{
// MAKE SURE WE ONLY UNLINK THE ERROR LOG FILE
$test = strrev($_POST['error_log']);
if (strpos($test, $error_log) === 0)
{
@unlink($path . $_POST['error_log']);
echo '<h3>' . $_POST['error_log'] . ' Discarded</h3>';
}
}
// COLLECT THE DIRECTORY INFORMATION OBJECTS
$objs = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
// ITERATE OVER THE OBJECTS
foreach($objs as $name => $obj)
{
// PROCESS THE ERROR LOG ONLY
$test = strrev($name);
if (strpos($test, $error_log) === 0)
{
// CREATE A DELETE BUTTON FOR THIS ERROR LOG
$name = substr($name, $plen);
$form = <<<EOD
<form method="post" style="margin:0; padding:0; display:inline;!important">
<b>$name</b>
<input type="submit" value="Discard?" />
<input type="hidden" name="error_log" value="$name" />
</form>
EOD;
echo $form;
// SHOW THE CONTENTS OF THIS ERROR LOG
echo '<pre>';
print_r(file_get_contents($path . $name));
echo PHP_EOL . '********** EOF **********';
echo '</pre>' . PHP_EOL;
}
}
// IF THERE ARE NO ERROR LOG(S)
$out = ob_get_contents();
if (empty($out)) echo '<h3>Good News! No error_log found.</h3>';
// SHOW THE GIT BRANCH
$root = '.git/HEAD';
$text = @file_get_contents($root);
if ($text)
{
$text = explode(DIRECTORY_SEPARATOR, $text);
$text = array_slice($text, 2);
$name = implode(DIRECTORY_SEPARATOR, $text);
echo PHP_EOL . "On Git branch: $name" . PHP_EOL;
}
else
{
echo PHP_EOL . "On Git branch: UNKNOWN" . PHP_EOL;
}
echo '<a href="' . $_SERVER['REQUEST_URI'] . '">Run Again</a>?' . PHP_EOL;
// SCRIPT TERMINATION WILL FLUSH THE OUTPUT BUFFER TO THE CLIENT BROWSER
<?php phpinfo();
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented:Some time ago when I published articles, there was a cascade of approvals - something like "It's OK" then "It's good" then "It's really good" and each of these approvals gave some more points as well as comments about how the article can be made better. Does E-E still do that?