简体   繁体   中英

get value from stored procedure in php

This is my SP:

DELIMITER $$

CREATE DEFINER=FUNCTION `test`(p_begin varchar(10),p_end varchar(10),p_code varchar(2)) RETURNS varchar(10) CHARSET latin1
BEGIN
    DECLARE V_ADA VARCHAR(8);
    DECLARE V_LAST VARCHAR(8);
    DECLARE V_NIK  VARCHAR(8);

    select NIK INTO V_NIK from absen where join_date >= p_begin and join_date<= p_end AND company_id=p_code ORDER BY NIK DESC LIMIT 1 ;

    SET V_NIK=V_NIK+1;
    return V_NIK;
END

I am trying to get the return value with php:

$query=$this->db->query("select test(\"$begin\",\"$end\", \"$code\")")->result();

    var_dump($query);
    die;

The result is:

array(1) { [0]=> object(stdClass)#20 (1) { ["test("2007-01-01","2007-12-31", "1")"]=> string(7) "118" } } 

My problem is that I want to get the value from stored procedure ("118") and I don't know how to change the object into a string.

如果您需要将对象作为文本发送到其他地方(数据库,Javascript代码),您可以选择序列化,json_encode,XML转换等等,具体取决于您的具体情况。

Try this:

$query=$this->db->query("select test(\"$begin\",\"$end\", \"$code\")")->result();

// Cast object to array (also you can try get_object_vars() to do that)
$query = (array)$query[0];

// Get the last value from array (it the only on so it is OK)
echo end($query);

Perhaps will help..

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