如何从PHP中的MySQL案例查询中检索结果集

时间:2015-11-21 15:47:29

标签: php mysql

到目前为止,我一直都在寻找这个问题的答案,但还是找不到任何答案。 我有以下MySQL PDO查询:

   class storeController : UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

        var list = [SKProduct]()
        var p = SKProduct()



        func buyProduct(){
            print("buy \(p.productIdentifier)")
            var pay = SKPayment(product: p)
            SKPaymentQueue.defaultQueue().addTransactionObserver(self)
            SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
        }

        override func viewDidLoad() {

            if(SKPaymentQueue.canMakePayments()) {
                print("IAP is enabled, loading...")
                var productID : NSSet = NSSet(object: "myapp.iap.500coins")
                var request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
                request.delegate = self
                request.start()
            } else {
                print("please enable IAPs")
            }

        }

        func requestDidFinish(request: SKRequest) {

        }

        func request(request: SKRequest, didFailWithError error: NSError) {

        }

        func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])    {
            print("add payment")

            for transactions:AnyObject in transactions {
                var trans = transactions as! SKPaymentTransaction
                print(trans.error)

                switch trans.transactionState {

                case .Purchased:
                    print("unlock iap here")
                    print(p.productIdentifier)

                    let prodID = p.productIdentifier as String
                    switch prodID {
                    case "myapp.iap.500coins" :
                        print("give coins")
                    default:
                        print("didnt go through")
                    }
                    queue.finishTransaction(trans)
                    break;
                case .Failed:
                    print("error")
                    queue.finishTransaction(trans)
                    break;
                default:
                    print("default")
                }
            }
            }

        func finishTransaction(trans:SKPaymentTransaction) {
            print("finished trans")
        }

        func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
            print("remove trans")
        }

        func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
            print("Product Request")
            let myProduct = response.products

            for product in myProduct {
                print("product found")
                print(product.productIdentifier)
                print(product.localizedTitle)
                print(product.localizedDescription)
                list.append(product)
            }
        }
    }

当我在PHPMyAdmin中使用recip_id = 18作为示例时,给出了下表:

mysql results table

我已经尝试了几种方法来获取php中的结果行,这样我就可以将这些值用于其他任务,但无济于事。我试过这个:

$q = "select recip_id,

sum(case when msg_read = '0' AND msg_deleted = '0' then 1 else 0 end) uu,
sum(case when msg_read = '0' AND msg_deleted = '1' then 1 else 0 end) ud,
sum(case when msg_read = '1' AND msg_deleted = '0' then 1 else 0 end) ru,
sum(case when msg_read = '1' AND msg_deleted = '1' then 1 else 0 end) rd,
count(*) as total

from messages where recip_id = :d GROUP BY recip_id WITH ROLLUP";`

而且:

$stmt = $dbo->prepare($q);
$row = $stmt->execute(array(":id" => $id));  
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud']; 
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";

这也是:

$stmt = $dbo->prepare($q);
$stmt->bindParam(":id", $id);
$msgcount = array();
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {   
        $total = $row->total;
    $uu = $row->uu;
    $ud = $row->ud; 
    $ru = $row->ru;
    $rd = $row->rd;
     }
    echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";

我无法使用MySQL结果集中的PHP脚本检索值。我已尝试对行和整个结果集进行serialize(),我尝试使用PDO :: FETCH_ASSOC以及未指定的fetch()和fetchAll()。 *使用了不同的组合,只得到一个空的结果集或*我无法在任何地方找到答案。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

在您第一次尝试时,您会忘记$stmt->fetch,在最后两次忘记$stmt->execute。试试这个:

$stmt = $dbo->prepare($q);
if ( ! $stmt->execute(array(":id" => $id)) ) die("error");  // returns true or false
$row = $stmt->fetch();                                      // add this line!
$total = $row['total'];
$uu = $row['uu'];
$ud = $row['ud']; 
$ru = $row['ru'];
$rd = $row['rd'];
echo "Recipient id: $id, Total: $total, UU: $uu, UD: $ud, RU: $ru, RD: $rd";