[英]mysqli and query with multiple rows
I am connecting xcode to webservices using, ASIHTTPRequest, and the mysqli prepare statement and also JSON. 我正在使用ASIHTTPRequest和mysqli prepare语句以及JSON将xcode连接到Web服务。
Whatever i do i get only one single Mysql-record in xcode as a result. 无论如何,我在xcode中只能得到一个Mysql记录。 I have looked everywhere and i have used Ray Wenderlich's "Promo code" example.
我到处都看过,并使用了雷·温德利希(Ray Wenderlich)的“促销代码”示例。 I guess i have to learn a bit here, but i just cannot find the answer.
我想我必须在这里学习一点,但我只是找不到答案。 Who can point me into the right direction?
谁能指出我正确的方向?
Thank you in advance, 先感谢您,
See the code below 见下面的代码
// 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();
I finally have found the solution for the question, of course with help of two of you. 我终于找到了这个问题的解决方案,当然要在你们两个的帮助下。 I think the solution need a bit more clarification.
我认为解决方案需要更多澄清。 The best way to do that is to post the code which worked for me, here it is,
最好的方法是发布对我有用的代码,就是这样,
// 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.