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
Release: 2024-12-13 02:46:09
Original
238 people have browsed it

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];
}
Copy after login

Using MySQL:

Incorrect Method:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template