简体   繁体   中英

Php Mysql Stored Procedure IN OUT

case: stored procedure looking like:

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: I want tot call the procedure and store the 4 returned objects into 4variables

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>

I always get zero rows back

<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>

Do I have to run 2 queries, a call to the procedure and a select on that result?
Is there a way to run it in one line?
... please help I've nowhere found a solution to my problem, already tried return in the procedure, but this is only allowed in functions. already 2weeks gone... learned a lot by reading/trial and error ;-), but nothing cracked this case....

Not sure why you want to be passing out 4 variables when you could simply return a resultset. This would also avoid you having to change the sproc interface everytime you want to extend the data you output.

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();
?>

It would also be worth your while cleaning up your schema so you have more appropriate integer datatypes for your keys (not everything is a signed integer 4 bytes) and avoid the mix of fieldnames you seem to have chosen for the same thing ie Id, ID, City_Id, City_ID etc. which will avoid all that unnecessary aliasing !

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

MySQL revised schema

  • removed the tbl tablename prefix
  • chosen more appropriate integer datatypes for your keys
  • renamed your key fields so they are consistent ie city_id

Tables

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;

Stored Procedure

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 ;

Hope this helps :)

use multi_query function

$mysqli->multi_query($query)

look on example

PHP + MySql + Stored Procedures, how do I get access an "out" value?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM