4

to call a stored procedure from php

 2 years ago
source link: https://www.codesd.com/item/to-call-a-stored-procedure-from-php.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

to call a stored procedure from php

advertisements

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:

  • why does my result return nothing?
  • what data type does my output variable @userid need to be?

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

Update

$result = $base->query("CALL get_id($id)");
while($data = $result->fetch_array(MYSQL_ASSOC)){
   echo $data['column_name'];
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK