2013-01-08 85 views
3

我試圖使用PDO調用SP(存儲過程)。PHP PDO:存儲過程返回空結果

try { 
    // Connecting using the PDO object. 
    $conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);   

    $stmt = $conn->prepare('CALL sp_user(?,?,@user_id,@product_id)');  
    $stmt->execute(array("demouser", "demoproduct")); 
    $result = $stmt->fetchAll(); 
    print_r($result); 
} 
// Catching it if something went wrong. 
catch(PDOException $e) { 
    echo "Error : ".$e->getMessage(); 
} 

SP成功執行並將數據插入到相關表中,並假設返回新插入的ID。但是當我打印結果時,我得到一個空數組。

有什麼建議嗎?

下面是我使用SP:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `test`.`sp_user`$$ 
CREATE PROCEDURE `sp_user`(
    IN user_name VARCHAR(255), 
    IN product_name VARCHAR(255), 
    OUT user_id INT(11), 
    OUT product_id INT(11) 
) 
BEGIN  
    START TRANSACTION;  
     INSERT INTO `user` (`name`) VALUES(user_name); 
     SET user_id := LAST_INSERT_ID();   
     INSERT INTO `product` (`name`) VALUES(product_name);   
     SET product_id := LAST_INSERT_ID();   
     INSERT INTO `map_user_product` (`user_id`,`product_id`) VALUES(user_id,product_id); 
    commit; 
END$$ 
DELIMITER ; 

編輯:沒關係。

我想通過使用$ result變量我將能夠獲取OUT變量的值。但後來我發現我需要使用另一個SQL查詢來獲取這些變量。

$stmt = $conn->query("SELECT @user_id,@product_id"); 

回答

0

您可以使用此得到最後插入的ID:PDO::lastInsertId();

我認爲你是目前從fetchAll,我不認爲返回數組包含ID:http://php.net/manual/en/pdostatement.fetchall.php

+0

我通過SP返回兩個ID(USER_ID和PRODUCT_ID)。我不認爲你的解決方案會在這種情況下工作,或將?換句話說,我想使用PDO獲取OUT變量....我該怎麼做? – neeraj

+0

我沒有看到你的SP在哪裏實際返回這些ID – 2013-01-08 08:16:35

+0

看看SET user_id:= LAST_INSERT_ID();和SET product_id:= LAST_INSERT_ID();而那些變量是OUT變量。 – neeraj

1

試這...

try 
{ 
    // Connecting using the PDO object. 
    $conn = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);   

    $stmt = $conn->prepare('CALL sp_user(:demouser, :demoproduct, @user_id, @product_id)'); 

    $demouser = "demouser"; 
    $demoproduct = "demoproduct"; 

    $stmt->bindParam(":demouser", $demouser, PDO::PARAM_STR, 255); 
    $stmt->bindParam(":demoproduct", $demoproduct, PDO::PARAM_STR, 255); 
    $stmt->execute(); 

    $result = $conn->query("select @user_id, @product_id;")->fetchAll(); 

    print_r($result); 

    foreach($result as $row) 
    { 
     echo $row["@user_id"]; 
     echo $row["@product_id"]; 
    } 

} 
// Catching it if something went wrong. 
catch(PDOException $e) 
{ 
    echo "Error : ".$e->getMessage(); 
} 
0

試試這個

DELIMITER $$ 
DROP PROCEDURE IF EXISTS `test`.`sp_user`$$ 
CREATE PROCEDURE `sp_user`(
    IN user_name VARCHAR(255), 
    IN product_name VARCHAR(255) 
) 
BEGIN  
    START TRANSACTION;  
     INSERT INTO `user` (`name`) VALUES(user_name); 
     SET user_id := LAST_INSERT_ID();   
     INSERT INTO `product` (`name`) VALUES(product_name);   
     SET product_id := LAST_INSERT_ID();   
     INSERT INTO `map_user_product` (`user_id`,`product_id`) VALUES(user_id,product_id); 
    commit; 

    SELECT user_id, product_id; 

END$$ 
DELIMITER ; 

在PHP

$result = $conn->query("CALL sp_user(demouser, demoproduct)")->fetchAll(); 
var_dump($result);