Home >Database >Mysql Tutorial >How Can I Call a MySQL Stored Procedure from PHP?

How Can I Call a MySQL Stored Procedure from PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-12-13 02:46:09241browse

How Can I Call a MySQL Stored Procedure from PHP?

Invoking a MySQL Stored Procedure via PHP Code

Problem:

A MySQL stored procedure named getTreeNodeName has been created, and the task is to invoke it using PHP code.

Solution:

Using MySQLi:

// Enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Connect to database
$connection = mysqli_connect("hostname", "user", "password", "db", "port");

// Run the stored procedure
$result = mysqli_query($connection, "CALL getTreeNodeName");

// Loop the result set
while ($row = mysqli_fetch_array($result)) {
    echo $row[0] . " - " . + $row[1];
}

Using MySQL:

Incorrect Method:

// DO NOT USE THIS METHOD
$connection = mysql_connect("hostname", "user", "password", "db", "port");

This method is deprecated and may cause errors.

Note:

It's recommended to use mysqli instead of mysql for improved stability and performance.

The above is the detailed content of How Can 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