Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I format an error message and prevent going out off page while it occures? 2

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
0
0
IL
Hi everyone,
Here is a simple PHP code aimes to connect into a MySql server.
I'm trying to format error messages in a way my page will continue working despite errors that occure.
Code:
<?php
$con=mysqli_connect("localhost","root","root4qpines","tivonidb");
if (mysqli_connect_errno($con))
  {
       echo
       '<p class="SQLerror">';
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            echo
        '</p>';
  }
else
  {
       echo
       '<p class="SQLerror">';
         echo "Success connecting to MySQL! " ;
         echo 
       '</p>';
  }
?>
Here's the css code:
Code:
p.SQLerror
	{
		width:447px;
		height:363px;
		background-color: #fd0303;
	}
In return I get error message that takes me out of my page and is inconsistent with the css
 
Put the error code in the html for the page rather than before the page outputs starts to be served.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
There are different types of errors. Fatal and non Fatal. Usually Fatal errors will stop execution as the code cannot continue after those errors.

What exactly are you getting instead of your styled error?

Also where in your page is this error? As Chris suggests if the PHP code runs before the HTML and by extension the CSS is loaded you probably won't get your styled error message.

Also if your PHP installation is set to display errors it will do so regardless of what you code. You can append a "@" (at symbol) to your function calls to suppress error display, but for production servers its best to set PHP.ini to not display errors, and instead have it log them to a file you can check later.

Note that if your let your page continue, and the rest of your code is dependent on the mysql connection you may get additional errors further down. Its best to maybe stop execution at that point of the PHP script and simply echo out a user friendly notice.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you both. I'll try your suggestions later, hopefully I'll not come back with further questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top