Php Mysql 存储过程 IN OUT

标签 php mysql mysqli procedure

案例:存储过程如下:

DROP PROCEDURE IF EXISTS `XofferCommon`.`getCdpc`;<br>
DELIMITER $$<br>
CREATE PROCEDURE `XofferCommon`.`getCdpc` (IN in_city_ID INT, OUT out_country_id INT, OUT out_district_id INT, OUT out_provence_id INT, OUT out_city_id INT)<br>
BEGIN<br>
DECLARE city_ID INT DEFAULT in_city_ID;<br>
SELECT t.Id, d.Id, p.Id, c.Id INTO out_country_id, out_district_id, out_provence_id, out_city_id FROM ((tblCity AS c INNER JOIN tblProvence AS p ON c.tblProvence_Id = p.ID) INNER JOIN tblDistrict AS d ON p.tblDistrict_Id = d.ID) INNER JOIN tblCountry AS t ON -d.tblCountry_Id = t.ID WHERE c.id = city_ID;<br>
END$$<br>
DELIMITER ;<br><br>
<br>---------------------------------------------------
Input: int referring to in_city_id<br>
Output: 4 x int country district provence and city id's<br>
---------------------------------------------------<br>

Php:我想调用该过程并将 4 个返回的对象存储到 4 个变量中

if ($res = $mysqli->query( 'CALL getCdpc(1321,@co,@di,@pr,@ci);SELECT @co,@di,@pr,@ci'))<br>
{<br>
    while($row = $res->fetch_object())<br>
    {<br>
        print($row);<br>
    }<br>
    $res->close();<br>
}<br>
else<br>
{<br>
    print "<br> no result given <br>";<br>
}<br>
<br>
Result:<br>
-------<br>

我总是得到零行

<br>
<br>
More info:<br>
----------<br>
in mysql sql the results get returned when I do:<br>
->call xoffercommon.getCdpc(1321,@co,@di,@pr,@ci);<br>
->select @co,@di,@pr,@ci;<br>
=> @co @di @pr @ci<br>
   1   2   3   4<br>
<br>
Question:<br>
---------<br>

我是否必须运行 2 个查询、对过程的调用以及对该结果的选择?
有没有办法在一行中运行它?
...请帮助我无处找到问题的解决方案,已经尝试在程序中返回,但这仅在函数中允许。 已经两周过去了...通过阅读/反复试验学到了很多东西;-),但是这个案子还没有破解...

最佳答案

不知道为什么当您可以简单地返回结果集时要传递 4 个变量。这还可以避免您每次想要扩展输出的数据时都必须更改存储过程接口(interface)。

MySQL

drop procedure if exists XofferCommon.getCdpc;

delimiter #

create procedure XofferCommon.getCdpc
(
in p_city_id int unsigned
)
begin
    select
     t.Id as country_id, 
     d.Id as district_id, 
     p.Id as provence_id, 
     c.Id as city_id
    from 
     tblCity c
     inner join tblProvence p on c.tblProvence_Id = p.Id 
     inner join tblDistrict d on p.tblDistrict_Id = d.Id 
     inner join tblCountry t on d.tblCountry_Id = t.Id 
    where
     c.Id = p_city_id;
end#

delimiter ;

PHP

<?php

ob_start(); 

try
{
    $db = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);

    if ($db->connect_errno) 
        throw new exception(sprintf("Could not connect: %s", $db->connect_error));

    $sqlCmd = sprintf("call getCdpc(%d)", 1);

    $result = $db->query($sqlCmd);

    if(!$result) throw new exception(sprintf("Invalid query : %s", $sqlCmd));

    if($result->num_rows <= 0){
        echo "no records found !";
    }
    else{
        $row = $result->fetch_assoc();
        echo sprintf("country_id = %d district_id = %d, provence_id = %d, city_id = %d",
            $row["country_id"],$row["district_id"],$row["provence_id"],$row["city_id"]);
    }
    $db->next_result(); 
    $result->close();
}
catch(exception $ex)
{
    ob_clean(); 
    echo sprintf("zomg borked - %s", $ex->getMessage());
}

if(!$db->connect_errno) $db->close();
ob_end_flush();
?>

清理架构也是值得的,这样您的键就有更合适的整数数据类型(并非所有内容都是带符号的 4 字节整数),并避免您似乎为同一事物选择的字段名称的混合,即Id、ID、City_Id、City_ID 等,这将避免所有不必要的别名!

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

MySQL 修改架构

  • 删除了 tbl 表名前缀
  • 为您的键选择更合适的整数数据类型
  • 重命名您的关键字段,使它们保持一致,即 city_id

表格

drop table if exists country;
create table country
(
country_id tinyint unsigned not null auto_increment primary key -- 0 to 255 countries;
)
engine=innodb;

drop table if exists district;
create table district
(
district_id smallint unsigned not null auto_increment primary key, -- 0 to 65535 districts
country_id tinyint unsigned not null
)
engine=innodb;

drop table if exists provence;
create table provence
(
provence_id smallint unsigned not null auto_increment primary key, - 0 to 65535 provences
district_id smallint unsigned not null
)
engine=innodb;

drop table if exists city;
create table city
(
city_id mediumint unsigned not null auto_increment primary key, - 0 to 16777215 cities
provence_id smallint unsigned not null
)
engine=innodb;

存储过程

drop procedure if exists getCdpc;

delimiter #

create procedure getCdpc
(
in p_city_id mediumint unsigned
)
begin
    select
     t.country_id, 
     d.district_id, 
     p.provence_id, 
     c.city_id
    from 
     city c
     inner join provence p on c.provence_id = p.provence_id
     inner join district d on p.district_id = d.district_id
     inner join country t on d.country_id = t.country_id
    where
     c.city_id = p_city_id;
end#

delimiter ;

希望这有帮助:)

关于Php Mysql 存储过程 IN OUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4942035/

相关文章:

php - mysql_insert_id();返回零。自动增量工作正常

mysql - BETWEEN 查询的性能较差

php - 用php显示mysql表

php - 数据库中每行的复选框值添加不正确

php - 如何使用 codeigniter 连接到不同服务器中的数据库

php - 对相关表数据进行排序

javascript - 使用 php mysql jquery ajax 动态更新选择框

php - MySQL/PHP - 在数字周围加上引号会破坏任何查询吗?

mysql - 使用中间表的另一个数据库中另一个表的列的部分数据更新列

php - 使用插入查询的 id