Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 390
Belajar Avatar asked Mar 07 '26 16:03

Belajar


2 Answers

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

like image 83
Bogdan Burym Avatar answered Mar 10 '26 07:03

Bogdan Burym


If you need to send the object as text to somewhere else (database, Javascript code), you have a choice of serialize, json_encode, XML conversion, and many more, depending on your exact situation.

like image 43
Abhishek Saha Avatar answered Mar 10 '26 05:03

Abhishek Saha