I am connecting xcode to webservices using, ASIHTTPRequest, and the mysqli prepare statement and also JSON.
Whatever i do i get only one single Mysql-record in xcode as a result. I have looked everywhere and i have used Ray Wenderlich's "Promo code" example. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.