php用pdo调用mysql存储过程 带参数 带返回值
下面是一个简单的MySQL存储过程示例:CREATE PROCEDURE `get_employee`(IN id INT, OUT name VARCHAR(50), OUT age INT)
BEGIN
SELECT `name`, `age` INTO name, age FROM `employee` WHERE `id` = id;
END
以上存储过程接受一个id参数,并从employee表中查询出该id对应的name和age,然后把它们赋值给name和age输出参数。
PHP中调用MySQL存储过程
在PHP代码中调用MySQL存储过程很简单。要调用存储过程,可以使用MySQLi扩展或PDO扩展提供的CALL语句。以下是使用MySQLi扩展调用MySQL存储过程的示例代码:
// 配置连接参数
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "yizuotu.net";
// 建立连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 调用存储过程
$id = 1;
$stmt = $conn->prepare("CALL get_employee(?, ?, ?)");
$stmt->bind_param("i", $id);
$stmt->bind_result($name, $age);
$stmt->execute();
$stmt->fetch();
$stmt->close();
echo "Name: ".$name.", Age: ".$age;