簡體   English   中英

使用CakePHP從MySQL存儲過程檢索輸出變量

[英]Retrieve output variable from MySQL stored procedure using CakePHP

我正在嘗試使用CakePHP調用存儲過程。

當前,返回的值由存儲過程中第一個SQL select語句的第一個記錄集組成。

即使在存儲過程中設置了輸出變量(即,將select @project_id into project_id ),它也不會顯示在查詢結果的var_dump中。

存儲過程:

CREATE DEFINER = 'admin'@'%'
PROCEDURE thebuggenie.cmdb_project_team_init(
  IN project_name VARCHAR(200), 
  IN project_key VARCHAR(200), 
  IN project_homepage VARCHAR(200), 
  IN team_name VARCHAR(200),
  OUT project_id INT(10))
BEGIN
  -- start transaction
  start transaction;

  -- init variables
  set @project_id = 0;
  set @team_id = 0;
  set @assoc_count = 0;
  set @scope_id = 1;

  -- select team and set variable
  select @team_id := id 
    from tbg3_teams 
    where name = team_name;

  -- if team_id = 0, insert team and set variable
  if @team_id is NULL or @team_id = '' or @team_id = 0 then
    -- insert new project
    insert into tbg3_teams(ondemand, name, scope) values(0, team_name, @scope_id);
    -- set team_id variable
    set @team_id = LAST_INSERT_ID();
  end if;

  -- select project and set variable
  select @project_id := id 
    from tbg3_projects 
    where name = project_name;

  -- if project_id = 0, insert project and set variable
  if @project_id is NULL or @project_id = '' or @project_id = 0 then
    -- insert project
    insert into tbg3_projects (name, locked, use_scrum, `key`, homepage, deleted, owner_team, scope, workflow_scheme_id, issuetype_scheme_id) values(project_name, 0, 1, project_key, project_homepage, 0, @team_id, @scope_id, 1, 1); 
    -- set project_id variable
    set @project_id = LAST_INSERT_ID();
  end if;

  select @assoc_count := count(*) 
    from tbg3_projectassignedteams 
    where uid = @team_id 
    and project_id = @project_id;

  if(@assoc_count = 0 and @project_id > 0 and @team_id > 0) then
    insert into tbg3_projectassignedteams (project_id, role_id, uid, scope) values(@project_id, 35, @team_id, @scope_id);
  end if;

  -- setup default views
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (101, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (102, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (110, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (105, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (106, 0, 0, @project_id, 2, 1);
  INSERT INTO tbg3_dashboard_views (name, view, pid, tid, target_type, scope) VALUES (111, 0, 0, @project_id, 2, 1);

  commit;

  -- return values
  select @project_id INTO project_id;
END

PHP代碼:

$sql = "call thebuggenie.cmdb_project_team_init(";
$sql .= '\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\'\'';
$sql .= ',\''.$results[0]['Repository']['team_name'].'\'';
$sql .= ',@project_id';
$sql .= ');';
$sql .= 'select @project_id as project_id';

var_dump($sql);

$results = $this->Asset->query($sql);

print_r($results);

PHP代碼輸出:

string 'call thebuggenie.cmdb_project_team_init('CMDB','CMDB','','team-app-platforms',@project_id);select @project_id as project_id;'

Array ( [0] => Array ( [0] => Array ( [@team_id := id] => 6 ) ) ) 

注意:我尚未完成錯誤陷阱。

我最終使用了這個:

$sql = "call thebuggenie.cmdb_project_team_init(";
$sql .= '\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\''.$results[0]['Asset']['project_name'].'\'';
$sql .= ',\'\'';
$sql .= ',\''.$results[0]['Repository']['team_name'].'\'';
$sql .= ',@project_id';
$sql .= '); select @project_id as project_id';

var_dump($sql);

$mysqli = new mysqli("DB_HOST", "DB_USER", "DB_PWD", "DATABASE");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$out = array();

if($mysqli->multi_query($sql))
 { 
    do
    {

        if($result=$mysqli->store_result())
        {
            while($row=$result->fetch_row())
            {
                array_push($out, $row);
            }
            $result->free();
        }

    }while($mysqli->more_results() && $mysqli->next_result());
 }


$mysqli->close();

$ out [3] [0]包含我想要的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM