2017-04-11 3 views
1

ここにいくつかのPHPコードがあります。私は自分のデータベースから行を取得しようとしているので、私はそれも私が作ったアプリで使うことができるようにJSON配列に変換します。私はDBOperations.phpからFunction.phpに配列を渡すのが難しいです。ここでPHPでこの関数に配列を返すには?

はDBOperations.phpのコードです:

//Read from diary table and enter results into an array 
public function readDiaryOperation($email) 
{ 
    $query = $this-> conn -> prepare("SELECT title,entry FROM diary WHERE email = :email"); 
    $query -> execute(array(':email' => $email)); 

    if($query->rowCount() > 0) 
    { 
     $data = $query->fetchAll(PDO::FETCH_ASSOC); 
     return json_encode($data); 
    } 
    else 
    { 
     $json['success'] = 0; 
     $json['message'] = 'No Data found';  
     $json['myintro'] = ''; 

     return json_encode($json); 
    } 
} 

そしてここでは、$メールが私のアプリからの換装を使用して設定されている私のfunctions.php

//Login user after checking that the user exists within the database or the  fields are empty 
public function readDiary($email) 
{ 
$db = $this -> db; 

if (!empty($email)) 
{ 
if ($db -> readDiaryOperation($email)) 
{ 
    $result = $db -> readDiaryOperation($email); 

    if(!$result) 
    { 
     $response["result"] = "failure"; 
     $response["message"] = "Read Diary Failure - Error Code 1"; 
     return json_encode($response); 
    } 
    else 
    { 
     $response["result"] = "success"; 
     $response["message"] = "Read Diary Operation Successful"; 
     $response["diary"] = $result; 
     return json_encode($response); 
    } 
} 
else 
{ 
    $response["result"] = "failure"; 
    $response["message"] = "Read Diary Failure - Error Code 2"; 
    return json_encode($response); 
} 
} 
else 
{ 
    return $this -> getMsgParamNotEmpty(); 
} 
} 

ためのコードです。

答えて

0

あなたは何を意味するのか、そのクエリが常に実行ダブルなり、二回あなたの関数のreadDiaryOperationを呼び出す:常にその状態ので、配列を返す関数readDiaryOperation、

if ($db -> readDiaryOperation($email)) 
{ 
    $result = $db -> readDiaryOperation($email); 

ところで:

if(!$result) 
{ 

ます常に偽りで実行されることはありません。

これは、見つかった行の数に関係なくです。コードは常に有効な結果を返します:

$response["result"] = "success"; 
$response["message"] = "Read Diary Operation Successful"; 
$response["diary"] = $result; 
return json_encode($response); 

そのような出力は必要ですか?

関連する問題