We help IT Professionals succeed at work.
Get Started

PHP graceful exit -- Catching mysql error in an XML output

hsguy asked
Last Modified: 2013-12-13
I'm trying to replace all the mysql_*() or die() constructs is a script and replace die with a more graceful -- and more useful exit upon an error.

Attached is my code using PHP 5 that does not do what I'd expect at all. The output contains an empty <message> tag.

Could someone point out what I'm doing wrong?

function errorXML($error_message){
		$xml_output = "<?xml version=\"1.0\"?>\n";
		$xml_output .= "<root>\n";
		$xml_output .= "\t<error>\n";
		$xml_output .= "\t\t<message>".$error_message."</message>\n";
		$xml_output .= "\t</error>\n";
		$xml_output .= "</root>";

function outputXML($xml_output){
		header("Content-type: text/xml");
		echo ($xml_output);

$linkID = @mysql_connect("badhost", "baduser", "badpass") or errorXML(mysql_error());
@mysql_select_db("badsite", $linkID) or errorXML(mysql_error());


Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE