2012-10-03 56 views
0

我想從php中的mysql存儲過程中獲得結果集。我怎樣才能做到這一點?任何幫助表示讚賞。最好是如果有人可以發佈代碼段! 例如,如果我有登錄表。我創建了一個存儲過程,而不會返回存儲過程中的結果在php

SELECT * FROM login 

現在我需要調用此存儲過程在PHP和遍歷結果集

+0

你爲什麼沒有[搜索這樣的東西,例如:Prepared statements and stored procedures](http://php.net/manual/en/) pdo.prepared-statements.php) –

+0

或訪問http://php.net/manual/en/book.pdo.php –

回答

0

PHP:

$mysqli = new mysqli("localhost", "root", "", "quadv2_dev"); 

if (!$mysqli->multi_query("CALL testlist()")) { 
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error; 
} 

do { 
    if ($res = $mysqli->store_result()) { 
     printf("---\n<pre/>"); 
     print_r($res->fetch_all()); 
     $res->free(); 
    } else { 
     if ($mysqli->errno) { 
      echo "Store failed: (" . $mysqli->errno . ") " . $mysqli->error; 
     } 
    } 
} while ($mysqli->more_results() && $mysqli->next_result()); 

的mysql:

DELIMITER $$ 

CREATE 
    /*[DEFINER = { user | CURRENT_USER }]*/ 
    PROCEDURE `quadv2_dev`.`testlist`() 
    /*LANGUAGE SQL 
    | [NOT] DETERMINISTIC 
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } 
    | SQL SECURITY { DEFINER | INVOKER } 
    | COMMENT 'string'*/ 
    BEGIN 

    SET @Qry ="SELECT * FROM Content"; 
    PREPARE st FROM @Qry; 
    EXECUTE st; 
    DEALLOCATE PREPARE st; 

    END$$ 

DELIMITER ;