如何执行mysqli多查询

时间:2015-11-11 01:22:54

标签: php xcode swift mysqli-multi-query

我似乎无法弄清楚我的mysqli多查询出了什么问题。当我运行它时,我在PHP端没有错误,但在Xcode中我得到一个致命的错误,它崩溃了。这是我在php端使用的代码来执行mysqli多查询:

$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));
}

就像我之前说的那样,我从这个脚本中得不到任何错误消息但是在Xcode中我在这段代码中收到错误:

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]

我不确定我做错了同样的Xcode代码在处理其他单个mysqli查询时对我有用,我不确定这是一个mysqi多查询的事实是否与它有关。任何帮助或建议将超级duper赞赏!谢谢!

更新:错误代码: 致命错误:'试试!'表达式意外地引发了错误:错误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,第50行

1 个答案:

答案 0 :(得分:1)

想出来!我没有将json编码数据发送回Xcode,从而制造了一个愚蠢的简单错误。我只需要添加:

echo json_encode($activity);

在多查询过程结束时如此

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

愚蠢的错误,但生病了,以防有人发现它有用!