开发者

Php script to continue if mysql fails

开发者 https://www.devze.com 2023-01-26 21:08 出处:网络
I have this code for an app i\'m working on: <?php header(\"Content-type: text/xml\"); //Gather data and prepare query

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']);
$开发者_如何学Cyhost = '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?


use ob_start() to ensure NO output return to browser/client when you performing database manipulation


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消