我试图使用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 ) ) )
注意:我还没有完成错误捕获。
答案 0 :(得分:0)
我最终使用了这个:
$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]包含我正在寻找的价值。