Home  >  Article  >  Backend Development  >  How Do I Call a MySQL Stored Procedure from PHP?

How Do I Call a MySQL Stored Procedure from PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-06 18:17:02365browse

How Do I Call a MySQL Stored Procedure from PHP?

Calling MySQL Stored Procedures from PHP

Invoking MySQL stored procedures from PHP code enables database operations to be executed efficiently. This article explores the process of calling a stored procedure within PHP, utilizing the MySQL client and server versions mentioned.

To call a stored procedure named getTreeNodeName with a parameter nid, follow these steps:

  1. Establish a connection to the MySQL database using PHP's MySQLi extension.
  2. Prepare and execute the SQL statement to call the procedure:
$result = mysqli_query($connection, "CALL getTreeNodeName($nid)");
  1. Fetch the result from the database:
while ($row = mysqli_fetch_array($result)){
    echo $row[0] . " - " . $row[1];
}

The above code snippet assumes that the getTreeNodeName procedure returns a single row with two columns. Adjust the code accordingly if different result structures are expected.

Note: The provided solution utilizes mysqli instead of the deprecated mysql_* functions, ensuring compatibility and better performance.

The above is the detailed content of How Do I Call a MySQL Stored Procedure from PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn