Php mysqli适用于sql,但不适用于存储过程

Php mysqli适用于sql,但不适用于存储过程,php,mysql,mysqli,Php,Mysql,Mysqli,上述方法非常有效,现在问题是在存储过程中使用相同的sql(测试的存储过程在命令行中工作良好)。如果我将sql字符串替换为 $table = "alabama"; $sql = "SELECT distinct zip FROM {$table} where zip is not null order by zip;"; $sql .= "SELECT distinct city FROM {$table} where city is not null order by city;"; $sql

上述方法非常有效,现在问题是在存储过程中使用相同的sql(测试的存储过程在命令行中工作良好)。如果我将sql字符串替换为

$table = "alabama";

$sql = "SELECT distinct zip FROM {$table} where zip is not null order by zip;";
$sql .= "SELECT distinct city FROM {$table} where city is not null order by city;";
$sql .= "SELECT distinct county FROM {$table} where county is not null order by county;";

$mysqli = new MySQLI('host','user','pass','db');
if ($mysqli->multi_query($sql)) {
       do {
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                 echo($row[0]);
            }
            $result->close();
        }
        if ($mysqli->more_results()) {
            echo ("next result");
        }
    } while ($mysqli->next_result());
}
$mysqli->close();
那么上面的脚本不会给出结果


我甚至用一个单独的存储过程进行了测试,它不会给出结果。

检查为in-mysql数据库授予的
procs_priv
列的权限->

我拼凑了一个简单但功能丰富的示例(不包括引用完整性),您可能会发现它很有用

完整脚本可在此处找到:

PHP 调用返回多个结果集的单个存储过程。当然,您可以只调用3个单独的存储过程

$sql = "CALL {$table}_zip();";
$sql .= "CALL {$table}_city();";
$sql .= "CALL {$table}_county();";

希望这有助于为您指出正确的方向:)

您没有为每个州设置一个表,是吗?我为每个州设置了一个表,然后我想我的下一个问题是:为什么您为每个州设置了一个表?谢谢,问题是用户的每个执行命令都被拒绝了。现在决定
<?php

$conn = new Mysqli("localhost", "foo_dbo", "pass", "foo_db");

$result = $conn->query(sprintf("call list_state_counties_cities(%d)", 1));
$row = $result->fetch_assoc();
echo $row["name"], "<br/>";

$conn->next_result();
$result = $conn->use_result();

while($row = $result->fetch_assoc()) echo $row["county_id"], "<br/>";

$conn->next_result();
$result = $conn->use_result();

while($row = $result->fetch_assoc()) echo $row["city_id"], "<br/>";

$result->close();   
$conn->close();

?>
-- TABLES

drop table if exists states;
create table states
(
state_id tinyint unsigned not null auto_increment primary key,
name varchar(255) unique not null,
county_counter smallint unsigned not null default 0
)
engine=innodb;

drop table if exists counties;
create table counties
(
county_id smallint unsigned not null auto_increment primary key,
state_id tinyint unsigned not null,
name varchar(255) not null,
city_counter smallint unsigned not null default 0,
key (state_id)
)
engine=innodb;

drop table if exists cities;
create table cities
(
city_id smallint unsigned not null auto_increment primary key,
county_id smallint unsigned not null,
state_id tinyint unsigned not null, -- denormalised shortcut join
name varchar(255) not null,
key (county_id),
key (state_id)
)
engine=innodb;

-- TRIGGERS

delimiter #

create trigger counties_before_ins_trig before insert on counties
for each row
begin
  update states set county_counter = county_counter+1 where state_id = new.state_id;
end#

create trigger cities_before_ins_trig before insert on cities
for each row
begin
declare v_state_id tinyint unsigned default 0;

  select state_id into v_state_id from counties where county_id = new.county_id;
  set new.state_id = v_state_id;

    update counties set city_counter = city_counter+1 where county_id = new.county_id;
end#

delimiter ;

-- STORED PROCEDURES

drop procedure if exists list_state_counties_cities;
delimiter #

create procedure list_state_counties_cities
(
in p_state_id tinyint unsigned
)
begin
    -- return multiple resultsets !!
    select * from states where state_id = p_state_id;
    select * from counties where state_id = p_state_id order by name;
    select * from cities where state_id = p_state_id order by name;
end #

delimiter ;

-- TEST DATA

insert into states (name) values ('state1'),('state2');

insert into counties (state_id, name) values 
(1,'county1'),(1,'county2'),(1,'county3'),
(2,'county4'),(2,'county5');

insert into cities (county_id, name) values
(1,'city1'),(1,'city2'),(1,'city3'),
(2,'city4'),(2,'city5'),
(3,'city6'),(3,'city7'),(3,'city8'),
(4,'city9'),(4,'city10'),
(5,'city11');

-- TESTING

select * from states;
select * from counties;
select * from cities;

call list_state_counties_cities(1);