Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli和多行查询_Php_Mysql_Ios_Json_Web Services - Fatal编程技术网

Php mysqli和多行查询

Php mysqli和多行查询,php,mysql,ios,json,web-services,Php,Mysql,Ios,Json,Web Services,我正在使用ASIHTTPRequest和mysqli prepare语句以及JSON将xcode连接到Web服务 因此,无论我做什么,我在xcode中只得到一条Mysql记录。 我到处都找过了,我使用了Ray Wenderlich的“促销代码”示例。 我想我必须在这里学点东西,但我就是找不到答案。 谁能给我指出正确的方向 提前谢谢大家, 请参见下面的代码 // Helper method to send a HTTP response code/message function sendResp

我正在使用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();

当然,在你们两位的帮助下,我终于找到了问题的答案。 我认为解决方案需要进一步澄清。 最好的办法是发布对我有用的代码,在这里

// 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
//发布用户信息的主要方法
函数getLevel(){
//检查所需的参数
如果(isset($_POST[“username”])){
//将参数放入局部变量
$usernameQ=$\u POST[“username”];
//发问
$stmt=$this->db->prepare('SELECT level_id,username,filename from mm_levels,其中username=?');
$stmt->bind_参数($s',$usernameQ);
$stmt->execute();
$arr=array();
$stmt->bind_result($lev_id、$username、$filename);
$i=0;
而($stmt->fetch())
{
$arr[]=array(“filename”=>$filename);//close();
sendResponse(200,json_编码($arr));
返回true;
}  
sendResponse(400,“无效请求”);
返回false;
}//getLevel
}//GetLevelAPI