【问题标题】:mysqli and query with multiple rowsmysqli和多行查询
【发布时间】:2013-09-19 21:55:52
【问题描述】:

我正在使用 ASIHTTPRequest 和 mysqli prepare 语句以及 JSON 将 xcode 连接到 Web 服务。

无论我做什么,结果我在 xcode 中只得到一个 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();

【问题讨论】:

    标签: php mysql ios json web-services


    【解决方案1】:

    我终于找到了问题的解决方案,当然是在你们两个的帮助下。 我认为解决方案需要更多说明。 最好的方法是发布对我有用的代码,在这里,

    // 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多