Garret Kaye
Garret Kaye

Reputation: 2562

how to perform a mysqli multi query

I can't seem to figure out what is going wrong with my mysqli multi query. When I run it I get no errors on the php side but in Xcode I get a fatal error and it crashes. This is the code I am using on the php side to perform the mysqli multi query:

$sql2 = "SELECT * FROM database.database WHERE SenderID='" . $userUsername . "'; ";
$sql2 .= "SELECT * FROM database.database WHERE Username='" . $userUsername . "'; ";
$sql2 .= "SELECT * FROM database.database WHERE SenderID='" . $userUsername . "'; ";

// Execute multi query
if (mysqli_multi_query($conn,$sql2))
{
do
    {
    // Store first result set
    if ($result2=mysqli_store_result($conn)) {
    // Fetch one and one row
    while ($row=mysqli_fetch_row($result2))
        {
        $activity[] = $row;
        }
    // Free result set
    mysqli_free_result($result2);
    }
    }
while (mysqli_next_result($conn));
}

Like I said before I get no error messages from this script but in Xcode I get an error in this code:

func retrieveActivity(latestMessage:String) {

    self.caseLoadBool = false

    let request = NSMutableURLRequest(URL: NSURL(string: "http://website/info.php")!)
    request.HTTPMethod = "POST"
    let postString = "string=\(self.userUsername)"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)", terminator: "")
            return
        }


        // Convert the json data into an array
        /* ERROR ON THIS LINE */ let dataArray:[AnyObject] = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! [AnyObject]

Im not sure what I am doing wrong this same Xcode code has worked for me when dealing with other single mysqli queries, I'm not sure if the fact that this is a mysqi multi query has anything to do with it. Any help or suggestions would be super duper appreciated! Thank you!

UPDATE: Error Code: fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang_PONDEROSA/swiftlang_PONDEROSA-700.1.101.6/src/swift/stdlib/public/core/ErrorType.swift, line 50

Upvotes: 0

Views: 510

Answers (1)

Garret Kaye
Garret Kaye

Reputation: 2562

Figured it out! I made a stupid simple error by not sending the json encoded data back to Xcode. I just had to add:

echo json_encode($activity);

at the end of the multi query process as so

// Execute multi query
if (mysqli_multi_query($conn,$sql2))
{
do
    {
    // Store first result set
    if ($result2=mysqli_store_result($conn)) {
    // Fetch one and one row
    while ($row=mysqli_fetch_row($result2))
        {
        $activity[] = $row;
        }
    // Free result set
    mysqli_free_result($result2);
    }
    }
while (mysqli_next_result($conn));


}

echo json_encode($activity);

Dumb mistake but ill leave the question up just in case someone finds it helpful!

Upvotes: 1

Related Questions