Php script to continue if mysql fails

你。 提交于 2020-01-07 03:17:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!