问题
I have this code for an app i'm working on:
<?php
header("Content-type: text/xml");
//Gather data and prepare query
$thequery = urlencode($_GET['s']);
$yhost = 'http://boss.yahooapis.com';
$apikey = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$url = $yhost.'/ysearch/news/v1/'.$thequery.'?appid='.$apikey.'&format=xml';
//Get the results
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace( array('<abstract>', '</abstract>'), array('<description>', '</description>'), $data);
$results = new SimpleXmlElement($data, LIBXML_NOCDATA);
//echo the results
echo $results->asXML();
?>
Where i have this line: $thequery = urlencode($_GET['s']);
i'd like to log this to a DB but its obviously very important that the XML gets outputted so how can i make sure, if the mysql bit fails for whatever reason, the XML still gets outputted? Or am i overcomplicating this?
回答1:
use ob_start() to ensure NO output return to browser/client when you performing database manipulation
回答2:
That's easy to solve, just prepend your mysql commands with an @ (or set error display to off) and check the return codes:
$ret = @mysql_query($query);
if(!$ret) {
do error handling;
} else {
continue with something else;
}
来源:https://stackoverflow.com/questions/4244796/php-script-to-continue-if-mysql-fails