2010-12-03 47 views
1

嘿,我刚做了一个iPhone应用程序,我让用户输入一个搜索词。然后,我将这个搜索词汇发送到我的虚拟主机上的一个PHP页面,然后我查询一些RSS提要并将xml返回到应用程序。我很有趣的做法是将用户搜索词记录到数据库中,然后像平常一样返回XML。显然更重要的是用户得到的是XML而不是记录的术语。那么,我可以添加到下面的代码,以确保XML安全地返回,而不管mysql是否失败?PHP安全地登录到mysql,但如果失败则继续脚本

任何想法?

<?php 
header("Content-type: text/xml"); 
//Gather data and prepare query 
$thequery = urlencode($_GET['s']); 
$yhost = 'http://boss.yahooapis.com'; 
$apikey = 'xxxxxxxxxxxxxxxxxxxxxxx'; 
$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); 
$results = new SimpleXmlElement($data, LIBXML_NOCDATA); 
//echo the results 
echo $results->asXML(); 
?> 

回答

1

你应该使用标准DB语法,如罚款:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); 
if ($link) { 
    // do your DB operations 
    mysql_close($link); 
} 

// output your XML 

这是什么会做的是试图连接到您的数据库和运行您的插入(记录)查询。无论失败如何,XML都将被输出。

+0

谢谢,刚刚实现了这一点,我的应用程序仍然有效。救济 – benhowdle89 2010-12-03 10:32:50

+0

很高兴帮助:) – SW4 2010-12-03 11:35:47

1

听起来像我之前已经回答重复的问题,

尝试输出缓冲

ob_start(); 
    /* code for database insertion */ 
ob_end_flush(); // ob_end_clean, ob_get_contents... 

细节:http://www.php.net/manual/en/ref.outcontrol.php

然而,用于记录用户搜索,做一个解析在Apache日志更容易(仅限GET),
以及使用较少资源优化页面优化的好处

已更新 ...同样的问题从你... Php script to continue if mysql fails