3

我在 mysql 中有一个带有输出参数的存储过程,我需要将结果发送到我的 php 中,该 php 使用了 codeigniter

注册模式

  public function registerUser($data){

            $sql = "call register_new_user('" . $data['uemail'] . "','" . $data['fname'] . "','".$data['lname'] . "','" . $data['pword']."',
            '" . $data['states'] . "',".$data['chargifyId'].");";

            if ($query = $this->db->query($sql)) {

            return  $this->db->query("Select @last_id");               

            } else {
                return false;
            }

    }

这是存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `register_new_user`(IN `uemail` VARCHAR(100), IN `fname` VARCHAR(45),
IN `lname` VARCHAR(45), IN `pword` VARCHAR(100), IN `states` VARCHAR(20), IN `chargifyid` INT, OUT `last_id` INT)
    NO SQL
BEGIN

if not exists (SELECT * from gi_user where 
(gi_user_email = uemail)) THEN

INSERT INTO gi_user(gi_user_email, gi_user_fname, 
gi_user_lname, gi_user_password,gi_user_status, 
gi_account_id, chargifyId) 
VALUES (uemail,fname,lname,pword,states,1,chargifyid);

SET last_id = LAST_INSERT_ID( ) ;

END IF;


END
4

3 回答 3

6

使用 CodeIgniter 事务

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete();

例子:

$SQL = "call UPDATE("
                . $ID.","
                . $this->db->escape($data["NAME"])."," // input
                . $this->db->escape($data["DESCRIPTION"])."," // input
                . " @out1" ."," // output
                . " @out2" //output
                .");";


        $this->db->trans_start();
            $this->db->query($SQL); // not need to get output
            $query = $this->db->query("SELECT @out1 as row_1,  @out2 as row_2");
        $this->db->trans_complete();

        $result = array();

        if($query->num_rows() > 0)
            $result = $query->result_array();

        print_r($result);
于 2013-12-22T08:12:33.533 回答
0
$out='';
$query ="begin register_new_user('" . $email . "','" .$name. "','".$lname ."','". $pword."','" . $states . "',:out); end;";
    $stmt = oci_parse($this->db->conn_id, $query );
    oci_bind_by_name($stmt, ":out", $out,300);

    oci_execute($stmt); 

    echo $out;

它为我工作。

于 2018-03-07T11:24:54.900 回答
0

工作示例!

--> SP

CREATE PROCEDURE calculateStock
(IN itemId INT(10), OUT stock INT(10))
BEGIN
    DECLARE purchased INT(10);
    DECLARE sold INT(10);

    SET purchased = (SELECT SUM(purchase_rows.qty) AS purchased FROM purchase_rows WHERE template_id = item_id);
    SET sold = (SELECT SUM(sale_rows.qty) AS sold FROM sale_rows WHERE template_id = item_id);

    SET stock = purchased - sold; 
    SELECT stock;
END

--> 型号

public function getStock($item_id){
    $query = "CALL calculateStock(?, @stock);";
    $parameters = array($item_id);

    $result = $this->db->query($query, $parameters);
    mysqli_next_result($this->db->conn_id); //Important Line 

    return $result->row()->stock;
}
于 2018-12-11T18:15:57.807 回答