0

在我的 android 应用程序中,我通过 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中以检查结果是否为null,我想用字符串值替换它“不存在”,在我的应用程序中我将处理它而不是处理空值将导致 JSON 异常,因为此格式中的空值或值无法解析为 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));}
     ?>
4

2 回答 2

0

尝试使用

$text = strip_tags($text);

为了这。这正是您所需要的。

http://www.php.net/manual/de/function.strip-tags.php

于 2012-05-15T06:55:30.800 回答
0

Few things to keep in mind, some of these are opinions but they may help:

<?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();
?>

When you return a JSON result back to, well, anything, it's helpful to make sure the structure of the JSON string has similar members.

If your query is successful:

{
  status: 'result',
  {
    data: [
      'bench press': '900kCal/hr'
    ],
    [
      'pullup': '1100kCal/hr'
    ]
  }
}

And if not:

{
  status: 'error',
  {
    data: [
      'reason': 'Database error.'
    ]
  }

This way, when you go to extract the data on the device, page, whatever your end-result is, you have one format to follow, i.e. getReason(), getResultCode(), etc.

于 2012-05-15T07:25:21.600 回答