how i can get output value from stored procedure to php variable ..
example Stored procedure :
DROP PROCEDURE `test`; CREATE DEFINER=`mydbinstance`#`%` PROCEDURE `test`(OUT `max` INT(11)) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
BEGIN
SELECT MAX(slno)+1 INTO #max FROM table1;
SELECT #max;
END
php:
$stmt=mysqli_query($conn,"CALL test();");
You should be able to get the result(s) of a stored procedure the same way you would any other query:
$stmt = mysqli_query($conn,"CALL test();");
if ($row = mysqli_fetch_row($stmt)) {
$value = $row[0];
}
Related
The SQL guy wrote a stored procedure that is suppose to return a list of item that'd be affected by the request.
If I call the stored procedure within SQL Server Management Studio like the example below, I can see the list in a column named after the alias
DECLARE #RETURN_CLIENTS_SET nvarchar(max)
EXEC [dbo].[Sp_IHM_SET_FORMS_INDPROD_CLOSE_DATE]
#Date = N'20171110',
#RETURN_ITEMS = #RETURN_ITEMS OUTPUT
SELECT #RETURN_ITEMS as N'#RETURN_ITEMS'
Now, If I try to show the result via a PHP like I'd do for a simple table fetch
$query_setdata = "DECLARE #RETURN_CLIENTS_SET nvarchar(max)
EXEC [dbo].[Sp_IHM_SET_FORMS_INDPROD_CLOSE_DATE]
#Date = N'20171110',
#RETURN_ITEMS = #RETURN_ITEMS OUTPUT
SELECT #RETURN_ITEMS as N'#RETURN_ITEMS'";
$prep_setdata = sqlsrv_prepare($conn, $query_setdata);
if(sqlsrv_execute($prep_setdata))
{
while($data = sqlsrv_fetch_array($prep_setdata,SQLSRV_FETCH_ASSOC)) {
echo $data['#RETURN_ITEMS'];
}
}
else {
die(print_r( sqlsrv_errors(), true));
}
The stored procedure does its stuff in the database (changing some flags for certain items), I have no error, but it doesn't show the "RETURN_ITEMS" value
Try a batch like this:
SET NOCOUNT ON;
DECLARE #RETURN_ITEMS nvarchar(max);
EXEC [dbo].[Sp_IHM_SET_FORMS_INDPROD_CLOSE_DATE]
#Date = N'20171110',
#RETURN_ITEMS = #RETURN_ITEMS OUTPUT;
SELECT #RETURN_ITEMS as RETURN_ITEMS;
The row count messages confuse some client drivers. The resultset column name should be an ordinary identifier, and the local variable you declare needs to be the one you return. And it can have the same name as the stored proc output parameter.
I'm trying to get a value from a stored procedure in php but I can't do it.
My stored procedure:
DROP PROCEDURE IF EXISTS sp_table;
DELIMITER $$
CREATE PROCEDURE sp_table()
BEGIN
SELECT COUNT(*) FROM table;
END$$
DELIMITER ;
My PHP code:
$recordSet_table = $conn->query("CALL sp_table()");
print_r($recordSet_table)."<br><br>";
Please try following code.
$sql = mysqli_query($connectionVariable,"CALL sp_table(#count)");
$result = mysqli_fetch_array($sql);
I never used stored procedures, but I faced a reality when I need to move my query to server. Please assist.
This is my function that I created via PHP:
function getResults($userid) {
$query = "select * from myTable where iduser= ?";
$stmt = $this->openDb()->prepare($query);
$stmt->bindValue(1, $userid, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
I never used stored procedures and not sure how to approach:
How do I add this as stored procedure to MySQL, so I could submit
one integer variable to it
How do I `prepare' the integer variable that I will submit to stored procedure
How do I retrieve the result set from stored procedure back to php
function.
If you could assist, I will review your solution and will be able to continue on my own.
I checked google and I see that to create a stored procedure I need to start like this:
CREATE PROCEDURE `getResults` (IN userid INT)
BEGIN
END
MYSQL:
DELIMITER $$
DROP PROCEDURE IF EXISTS getResults$$
CREATE PROCEDURE getResults(IN I_USERID INT(10))
BEGIN
SELECT * FROM `myTable` WHERE iduser = I_USERID;
END$$
DELIMITER ;
PHP:
$sql = "CALL getResults(?)";
$stmt = $this->openDb()->prepare($sql);
$stmt->execute(array($userid));
while ($row = $stmt->fetchObject()) {
//use fields like this: $row->fieldname1, $row->fieldname2...
}
$stmt->closeCursor();
On a side note, I would recommend naming explicitely your fields instead of using the * selector in your query.
Hope it helps,
S.
I have created the following stored procedure in mysql...
DELIMITER //
CREATE PROCEDURE GetMember(IN in_memberID int)
BEGIN
SELECT *
FROM Members
WHERE MemberID = in_memberID;
END//
$result = mysql_query("CALL GetMember(".$memberID.")") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $row['Name'] . "</br>";
}
But when I call it from php it returns all records in the Members table, what am I doing wrong?
EDIT:
When I try to call the query within phpmyadmin I get this error
CALL GetMember(1);
#1312 - PROCEDURE myDb.GetMember can't return a result set in the given context
what version of PHP?
PHP 5.2.3 and PHP 5.2.4 have a bug with procedures:
https://bugs.php.net/bug.php?id=42548
use Database_Name;
DELIMETER $$
DROP PROCEDURE IF EXISTS Proc$$
CREATE PROCEDURE Proc()
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 110000 DO
INSERT INTO Table_Name (word, mean) VALUES ('a', 'a mean');
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;
I've been trying to find an answer to this question, but haven't found any definitive "yes" or "no" in all my research.
I'm running a simple MySQL query like this:
UPDATE item SET `score`=`score`+1 WHERE `id`=1
Is there a way for that query to return the updated value, instead of the number of rows affected? Just as a reference, I'm doing this in PHP, so the actual code looks like:
$sql = "UPDATE item SET `score`=`score`+1 WHERE `id`=1";
$new_value = mysql_query($sql);
//Unfortunately this does not return the new value
I know I could do a second query and just SELECT the value, but I'm trying to cut down on queries as much as possible. Is there a way?
You can do it with a stored procedure that updates, and then selects the new value into an output parameter.
The following returns one column new_score with the new value.
DELIMITER $$ -- Change DELIMITER in order to use ; withn the procedure
CREATE PROCEDURE increment_score
(
IN id_in INT
)
BEGIN
UPDATE item SET score = score + 1 WHERE id = id_in;
SELECT score AS new_score FROM item WHERE id = id_in;
END
$$ -- Finish CREATE PROCEDURE statement
DELIMITER ; -- Reset DELIMITER to standard ;
In PHP:
$result = mysql_query("CALL increment_score($id)");
$row = mysql_fetch_array($result);
echo $row['new_score'];
No, there's nothing like postgresql's UPDATE ... RETURNING output_expression in MySQL (yet?).
If you don't want to run another Query SELECT then here is another way to do it. I have modified Mr. Berkowski code for reference:
DELIMITER $$
CREATE PROCEDURE increment_score
(
IN id_in INT
)
BEGIN
set #newScore := null;
UPDATE item SET score = IF((#newScore := score+1) <> NULL IS NULL, #newScore, NULL) WHERE id = id_in;
SELECT #newScore;
END
DELIMITER ;
No you cant. You could make a function or stored procedure that could do the insert and return the updated value but that would still require you to execute two queries from within the function or stored procedure.
You can create a trigger, and you will know everything about the modifications.