2012-05-15 31 views
0

在我的Android應用我通過PHP腳本返回從我的MySQL數據庫的一些數據,如果返回的結果爲空:如何從返回的php結果中刪除標籤?

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" 

這個詞:它會以這種格式返回給我的應用程序「不存在「我添加它在我的PHP通過if語句檢查結果,如果它的null,我想用」不存在「的字符串值替換它,並在我的應用程序中,我將處理它,而不是處理空值,這將導致JSON異常,因爲這個有價值的空值或值不能被解析爲JSON數組我的問題是:我如何刪除所有返回的結果標籤?如果$輸出爲空,我將它替換爲「不存在」,但它沒有以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));} 
    ?> 

回答

0

幾件事情要記住,其中也不乏一些意見,但他們可能會有所幫助:

<?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()等。