Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL代码在PHPmyADMIN或Navicat中运行时不会使用_Php_Sql_Mysql - Fatal编程技术网

SQL代码在PHPmyADMIN或Navicat中运行时不会使用

SQL代码在PHPmyADMIN或Navicat中运行时不会使用,php,sql,mysql,Php,Sql,Mysql,我有下面的代码,但它不断出现错误。代码的第一部分创建必要的表,后面是-应该创建存储过程的内容,但它没有 有什么想法吗 drop table if exists agent; create table agent ( agent_id int unsigned not null auto_increment primary key, name varchar(32) not null, commission_level tinyint unsigned default 0, parent_age

我有下面的代码,但它不断出现错误。代码的第一部分创建必要的表,后面是-应该创建存储过程的内容,但它没有

有什么想法吗

drop table if exists agent;

create table agent
(
agent_id int unsigned not null auto_increment primary key,
name varchar(32) not null,
commission_level tinyint unsigned default 0,
parent_agent_id int unsigned default null
)
engine = innodb;

insert into agent (name, commission_level, parent_agent_id) values

('I', 99, null),
  ('A', 7, 1),
  ('B', 6, 1),
    ('C', 5, 2),
    ('D', 6, 2),
    ('E', 5, 3),
    ('F', 2, 3),
      ('G', 5, 5),
      ('H', 1, 5);


delimiter ;

drop procedure if exists agent_hier;

delimiter #

create procedure agent_hier
(
in p_agent_id int unsigned
)
proc_main:begin

declare done tinyint unsigned default 0;
declare dpth smallint unsigned default 0;


create temporary table hier(
 parent_agent_id int unsigned, 
 agent_id int unsigned, 
 depth smallint unsigned default 0
)engine = memory;

insert into hier values (p_agent_id, p_agent_id, dpth);

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while done <> 1 do

    if exists( select 1 from agent a inner join hier on a.parent_agent_id = hier.agent_id and hier.depth = dpth) then

        insert into hier 
            select a.parent_agent_id, a.agent_id, dpth + 1 from agent a
            inner join tmp on a.parent_agent_id = tmp.agent_id and tmp.depth = dpth;

        set dpth = dpth + 1;            

        truncate table tmp;
        insert into tmp select * from hier where depth = dpth;

    else
        set done = 1;
    end if;

end while;


select 
 a.agent_id,
 a.name as agent_name,
 if(a.agent_id = b.agent_id, null, b.agent_id) as parent_agent_id,
 if(a.agent_id = b.agent_id, null, b.name) as parent_agent_name,
 hier.depth,
 a.commission_level
from 
 hier
inner join agent a on hier.agent_id = a.agent_id
inner join agent b on hier.parent_agent_id = b.agent_id
order by
 -- dont want to sort by depth but by commision instead - i think ??
 -- hier.depth, hier.agent_id; 
 a.commission_level desc;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end proc_main #


delimiter ;


/*

select * from agent;

call agent_hier(1);
call agent_hier(2);
call agent_hier(3);
call agent_hier(5);
*/

我尝试了你的SQL代码,并为我工作,我假设你的用户帐户没有正确的权限

试一试


并确保用户具有创建例程权限

我在这里为您制作了一个与navicrap兼容的脚本-如果您具有相关权限,现在应该可以正常运行:p

受影响的行:0
时间:0.001ms

@Brian-几个错误-我可以使用下面@f00的navicrap脚本解决问题。你能让它运行吗??蟾蜍是更好的顺便说一句,它是free@f00-在几个额外的mod之后运行了脚本,但是php导致了错误:查询失败,出现错误:过程db.agent\u hier无法在给定的上下文中返回结果集我昨天确实根据您的建议下载了Toad,但是我更喜欢navicrap的UI,觉得它使用起来更简单。也许我会再试一试。是的,这是一个php mysql扩展问题-此扩展的旧版本不支持调用存储过程,您需要使用mysqli扩展i来提高性能,尽管这是有争议的,或者升级您的php版本,使其具有最新的mysql扩展:明白了-无论如何,我一直在讨论是否要转移到mysqli。请注意,我正在运行PHP5.3.2
SHOW GRANTS;