0

ASIHTTPRequest、mysqli prepare ステートメント、および JSON を使用して、xcode を Web サービスに接続しています。

何をしても、結果として xcode で 1 つの Mysql レコードしか取得できません。私はあらゆる場所を調べ、Ray Wenderlich の「プロモーション コード」の例を使用しました。ここで少し学ばなければならないと思いますが、答えが見つかりません。誰が私を正しい方向に向けることができますか?

前もって感謝します、

以下のコードを参照してください

// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
    $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
    header($status_header);
    header('Content-type: ' . $content_type);
    echo $body;
}

class GetLevelAPI {
    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('localhost', 'root', '', 'madmagnets');
        $this->db->autocommit(FALSE);
    }

    // Destructor - close DB connection
   function __destruct() {
        $this->db->close();
    }

    // Main method to post user info 
    function getLevel() {

    // Check for required parameters
    if (isset($_POST["username"]))  {

    // Put parameters into local variables
        $usernameQ = $_POST["username"];

    // fire the query
        $stmt = $this->db->prepare('SELECT level_id, username, filename from 
                            mm_levels WHERE username=? ');

        $stmt->bind_param("s", $usernameQ ) ;
        $stmt->execute();
        $stmt->bind_result($id1, $username, $filename );
        while ($stmt->fetch()) {
            break;
        }
        $stmt->close();

    $result = array(
        "filename"  => $filename ,
        "username"  => $username ,
    );
    sendResponse(200, json_encode($result));
    return true;

}  
    sendResponse(400, 'Invalid request');
    return false;
} //getLevel
}  //GetLevelAPI

$api = new GetLevelAPI;
$api->getLevel();
4

1 に答える 1

0

もちろん、2 人の助けを借りて、私は最終的に質問の解決策を見つけました。解決策にはもう少し説明が必要だと思います。それを行う最善の方法は、私のために働いたコードを投稿することです。

// Main method to post user info 
function getLevel() {
    // Check for required parameters
    if (isset($_POST["username"]))  {

    // Put parameters into local variables
        $usernameQ = $_POST["username"];

    // fire the query
    $stmt = $this->db->prepare('SELECT level_id, username, filename from mm_levels WHERE username=? ');                         
    $stmt->bind_param("s", $usernameQ ) ;
    $stmt->execute();
    $arr = array();

    $stmt->bind_result($lev_id,$username, $filename);
    $i=0;
    while ($stmt->fetch())
    {
         $arr[] = array( "filename" => $filename );   // <= this line was the last addition and did the trick
         $i++;
    }

    $stmt->close();
    sendResponse(200,json_encode($arr));
    return true;
}  
    sendResponse(400, 'Invalid request');
    return false;
} //getLevel
}  //GetLevelAPI
于 2013-09-16T09:05:41.237 に答える