Avatar of hsguy
hsguy
 asked on

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

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?

Thanks
<?php
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>";
		outputXML($xml_output);
		exit();
}

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

PHP

Avatar of undefined
Last Comment
hsguy

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
theremon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
hsguy

ASKER
That's exactly what I expected.  Thanks for letting me know it worked. upon closer inspection I had this line:
require_once('./../includes/common.php');
Which I deleted out of the code I posted--not event hinking that it could be the problem.

When I commented that line out, it worked as expected.

So the problem lies somewhere in that common.php

Thanks for the help!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy