Jump to content

[SOLVED] PHP - echo issue


petersro

Recommended Posts

While working on the error handler i have an issue with it working but not putting things on a new line, any hints?

// set the error reporting level for this script
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

// Change defaults
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');

// set to the user defined error handler
set_error_handler("custom_err_handler");

// The path to the error log file
define('ERROR_LOG_PATH', './logs/error_log.txt');

// Our custom error handler function
function custom_err_handler($num, $str, $file, $line) {
global $error_log_path;
    $err = "";
    $err .= "---- PHP Error ----\n";
    $err .= "Number: [" . $num . "]\n";
    $err .= "String: [" . $str . "]\n";
    $err .= "File: [" . $file . "]\n";
    $err .= "Line: [" . $line . "]\n\n";
    error_log($err, 3, ERROR_LOG_PATH);
    echo $err;
}

 

Link to comment
https://forums.phpfreaks.com/topic/136019-solved-php-echo-issue/
Share on other sites

i tried the \r\n, it works on the logfile, but doesnt work on the php site, it still displays as normal

 

Logfile output

---- PHP Error ----
Number: [2]
String: [mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'root'@'localhost' (using password: YES)]
File: [C:\wamp\www\onepro\system\sys_mysql.php]
Line: [12]

---- PHP Error ----
Number: [1024]
String: [256]
File: [C:\wamp\www\onepro\system\sys_mysql.php]
Line: [12]

---- PHP Error ----
Number: [8]
String: [use of undefined constant db_name - assumed 'db_name']
File: [C:\wamp\www\onepro\system\sys_mysql.php]
Line: [13]

---- PHP Error ----
Number: [2]
String: [mysql_select_db(): supplied argument is not a valid MySQL-Link resource]
File: [C:\wamp\www\onepro\system\sys_mysql.php]
Line: [13]

---- PHP Error ----
Number: [1024]
String: [256]
File: [C:\wamp\www\onepro\system\sys_mysql.php]
Line: [13]

 

Website display

---- PHP Error ---- Number: [2] String: [mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES)] File: [C:\wamp\www\onepro\system\sys_mysql.php] Line: [12] ---- PHP Error ---- Number: [1024] String: [256] File: [C:\wamp\www\onepro\system\sys_mysql.php] Line: [12] ---- PHP Error ---- Number: [8] String: [use of undefined constant db_name - assumed 'db_name'] File: [C:\wamp\www\onepro\system\sys_mysql.php] Line: [13] ---- PHP Error ---- Number: [2] String: [mysql_select_db(): supplied argument is not a valid MySQL-Link resource] File: [C:\wamp\www\onepro\system\sys_mysql.php] Line: [13] ---- PHP Error ---- Number: [1024] String: [256] File: [C:\wamp\www\onepro\system\sys_mysql.php] Line: [13] 

thanks mate working on introducing it in my code, i have another question, would make things more cleaner, is there a way i can when

$db = mysql_select_db(db_name,$conn) or trigger_error(E_USER_ERROR);

trigger_error(E_USER_ERROR) is reached,

 

Is there a way to if there is a problem throw a custom error, secondly is there a way to get it to re-direct on error to say error.php on the root of the site, displaying the echo of whats wrong, in a clean html layout. im guessing the first is possable with a varable, but i dont know how to do the second part.

 

 

for custom error

<?php
$db = mysql_select_db(db_name,$conn);
if(!$db)
{
echo "Customer error message";
//or if you want to redirect
$error = "db error";
header("Location:error.php?error=".$error);
}
?>

then on error.php

<?php
if(isset($_GET['error']))
{
echo $_GET['error'];
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.