Reputation: 1520
I'm calling a stored procedure from php
<?php
include('global/db.php');
$id = 1;
$base = conn();
$query = "CALL get_id(:id,@userid)";
$stmt = $base->prepare($query);
$stmt->bindParam(':id',$id);
$stmt->execute();
print_r($stmt->fetch());
?>
the stored procedure looks like this
BEGIN
SET @userid = (SELECT * FROM user WHERE user.id = id);
SELECT @userid;
END
the procedure parameters
IN id int(10), OUT userid VARCHAR(255)
Questions:
@userid
need to be? Upvotes: 1
Views: 112
Reputation: 2765
Your stored procedure has some problems.
Since you have an output userid
why this @userid
.
Also you are outputting userid
, So there is no need to SELECT
it
Another thing is, you can't select the entire row into a single variable, for multiple columns you should use multiple output variables.
BEGIN
SET userid = (SELECT column_name FROM user WHERE user.id = id);
END
$result = $base->query("CALL get_id($id)");
while($data = $result->fetch_array(MYSQL_ASSOC)){
echo $data['column_name'];
}
Upvotes: 1
Reputation: 107567
Currently, you are defining a scalar parameter to entire query resultset as noted by asterisk. Consider using INTO
clause to pass one query result value into the OUT
parameter:
DELIMITER $$
CREATE PROCEDURE get_id (
IN id int(10),
OUT userid VARCHAR(255))
BEGIN
SELECT user.id
INTO userid
FROM user
WHERE user.id = id;
END$$
DELIMITER ;
And in PHP, you need to run a second query to return the @userid
variable in a fetch command:
try {
include('global/db.php');
$id = 1;
$base = conn();
$query = "CALL get_id(:id,@userid)";
$stmt = $base->prepare($query);
$stmt->bindParam(':id',$id);
$stmt->execute();
$result = $base->query("SELECT @userid")->fetchAll();
if ($result) { print_r($result); }
} catch (PDOException $pe) {
echo $pe->getMessage()."\n";
}
Upvotes: 1