MySQL存储过程的创建和调用

Mysql存储过程弱爆了。
下面这个创建代码,任何地方不匹配都报错,包括空格!
DELIMITER //
create procedure authappkey(in p1 varchar(32), in p2 varchar(32), in p3 varchar(32), in p4 varchar(45), out p5 int)
begin
	-- 权限检查
	-- p1:项目ename; p2:接口ename; p3:appkeyindex; p4:appkey
	-- by wuzude 2018-01-22 ver 1.0
    declare rawsum int(1) default 0;   -- 行计数器
    declare projid int(4) default 0;   -- 项目ID
    declare intfcid int(4) default 0;  -- 接口ID
	declare appkeyid int(11) default 0;  -- appkey序号
    -- 取得有效的项目ID和接口ID,对行计数
    select count(pi_id), proj_id, intfc_id into rawsum, projid, intfcid
    from proj_intfc
    where proj_id = (select proj_id from proj where ename = p1 and valid = 1)
    and intfc_id = (select intfc_id from intfc where ename = p2 and valid = 1)
    and valid = 1;
	-- select rawsum,projid,intfcid;

	if rawsum = 1 then
		set rawsum = 0;
		-- 取得有效的appkey_id(顺序号),对行计数
		select count(appkey_id), appkey_id into rawsum, appkeyid
		from appkeys
		where appkeyindex = p3
		and appkey = p4
		and valid = 1
		and ((unix_timestamp(now()) * 1000) < expiretime or remain_num > 0);
		
		if rawsum = 1 then
			set rawsum = 0;
			-- 查APPKEY的项目权限
			select count(power_id) into rawsum
			from  power_proj
			where appkey_id = appkeyid
			and   proj_id = projid
			and   valid = 1;
			
			if rawsum = 1 then
				set rawsum = 0;
				-- 查APPKEY的接口权限
				select count(power_id) into rawsum
				from power_intfc
				where appkey_id = appkeyid
				and intfc_id = intfcid
				and valid = 1;
				
				if rawsum = 1 then
					set p5 = 1;
				end if;
				
			end if;
			
		end if;
		
	end if;
	select p5;
end //
DELIMITER ;
在phpmyadmin中调用
-- 出参必须使用一个变量接住
call authappkey('ccav', 'avcc', '1fb997cc', '6191555132980973', @out) 
在PHP中调用
$sql = "call authenti($proj, $intfc, $appkeyindex, $appkey, @out)";
if($T->authenti($sql, "p5"))
{
    $activ = $K->kinjc($cnMysql, $K->kxss($activ));
    $content = $K->kinjc($cnMysql, $K->kxss($content));
    $toProj = array(&$keyid, &$keystr, &$interface, &$activ, &$content);
    //传递keyid、keystr、interface、content、type给项目接口脚本[projname].php
    $proj = str_replace("'", "", $proj);  //去掉单引号(否则引用为类名时会被再加一对单引号)
    $cls = new $proj($pData[1]);
    $cls->router($toProj);
    $rData = "嗨,来信收到,这些年你还好吗?";
    $R->send($rData);//返回数据
}
public function authenti($sql, $tag)
{
    $CN = $this->cn();
    $R = $this->r();
    $M = $this->m();
    if($CN->connect_error)
    {
        $R->send(1120);
        $M->error($this->ms() . "," . __FILE__ . ",Line:" . __LINE__ . ",authenti():" . $CN->connect_error);
    }
    $resource = $CN->query($sql);
    if($resource === false)
    {
        $R->send(1121);
        $M->error($this->ms() . "," . __FILE__ . ",Line:" . __LINE__ . ",authenti():cn->query($sql)=false");
    }
    $result = $this->obj2arr(mysqli_fetch_array($resource, MYSQLI_BOTH));  //转数组
    mysqli_free_result($resource);
    $CN->close();
    //return $result;
    if(!isset($result[$tag]))
    {
        $R->send(1122);
        $M->error($this->ms() . "," . __FILE__ . ",Line:" . __LINE__ . ",authenti():未找到名为" . "【" . $tag. "】" . "的key");
    }
    if($result[$tag] != 1)
    {
        $R->send(1123);
        $M->error($this->ms() . "," . __FILE__ . ",Line:" . __LINE__ . ",authenti():proj|intfc|appkeyindex|appkey错误");
    }
    return true;
}





你可能感兴趣的:(MySQL存储过程的创建和调用)