Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of theremon
theremon
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hsguy
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!