在我的安卓应用程序中,我通过 php 脚本从我的 MYSQL 数据库中返回一些数据,如果返回的结果为 null:它将以这种格式返回到我的应用程序:
result<br /><font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: output in C:'wamp'www'y.php on line <i>16</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>368264</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:'wamp'www'y.php' bgcolor='#eeeeec'>..'y.php<b>:</b>0</td></tr></table></font>" not exist"
这个词:"不存在" 我通过 if 语句将其添加到我的 PHP 中以检查结果是否为空,我想用"不存在"的字符串值替换它,在我的应用程序中,我将处理它而不是处理会导致 JSON 异常的空值,因为此 foramt 中的空值或值无法解析为 JSON 数组我的问题是:如何删除结果返回的所有标签? 如果$output为空,我将其替换为"不存在",但它没有以 JSON 格式返回,以防万一它有值,为什么? 请帮助我,因为我是PHP的新手...
PHP代码
$name=$_REQUEST['Name'];
mysql_connect("localhost","user","pass")or OnError('database connection failed', mysql_error());
mysql_select_db("MYDB")or OnError('database selection failed', mysql_error($mysql));
mysql_query("SET NAMES utf8");
$sql=mysql_query("select burnedCalories from Exercise where Name='$name' ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
if (is_null($output)) // if the exercise name not exist the result will be null
{
$output = ' not exist';
}
print(json_encode($output));
mysql_close();
define('DEBUG_DETAILS', true);
function onError($msg, $details) {
$msg = array(
'status'=>'error',
'message'=>$msg);
if ( defined('DEBUG_DETAILS') && DEBUG_DETAILS ) {
$msg['details'] = $details;
}
die(json_encode($msg));}
?>
尝试使用
$text = strip_tags($text);
为此,这正是您所需要的。
http://www.php.net/manual/de/function.strip-tags.php
要记住的几件事,其中一些是意见,但它们可能会有所帮助:
<?php
// Constants are usually defined first and foremost, or stored in classes and accessed statically.
define('DEBUG_DETAILS', true);
function onError($msg, $details) {
$msg = array('status'=>'error',
'message'=>$msg);
if( defined('DEBUG_DETAILS') && DEBUG_DETAILS ) {
$msg['details'] = $details;
}
die(json_encode($msg));
}
// try to use the specific request method, and always sanitize!
$name = filter_var($_POST['Name'], FILTER_SANITIZE_STRING);
/* Best to put this into a separate file, but not necessary, of course.
Handling MySQL errors should fail a bit more gracefully in a "live" environment,
such as setting the 'error' field to your $output array to 'true'.
*/
mysql_connect("localhost","user","pass") or OnError('database connection failed', mysql_error());
mysql_select_db("MYDB") or OnError('database selection failed', mysql_error());
mysql_query("SET NAMES utf8");
$sql = mysql_query("SELECT burnedCalories FROM Exercise WHERE Name='$name'");
if(!$sql){
// clear the $output by assigning an array
onError("The query failed.", mysql_error());
}
// set the status
$output['status'] = 'result';
// otherwise... perform usual loop
while($row = mysql_fetch_assoc($sql))
$output[] = $row;
print(json_encode($output));
mysql_close();
?>
当您将 JSON 结果返回给任何内容时,确保 JSON 字符串的结构具有类似的成员会很有帮助。
如果查询成功:
{
status: 'result',
{
data: [
'bench press': '900kCal/hr'
],
[
'pullup': '1100kCal/hr'
]
}
}
如果没有:
{
status: 'error',
{
data: [
'reason': 'Database error.'
]
}
这样,当您在设备,页面上提取数据时,无论您的最终结果是什么,您都可以遵循一种格式,即 getReason()
、getResultCode()
等。