使用MVC 架構在phpMyAdmin 中建立和呼叫預存程序
在phpMyAdmin 中,您可以方便地在資料庫的“例程”選項卡中建立預存程序。以下是編寫和調用存儲過程的分步指南:
創建存儲過程:
使用MVC 呼叫預存程序:
在MVC 架構中,您可以從Model 類別呼叫預存程序:
<code class="php">// Model class (e.g., ProcedureModel.php) class ProcedureModel extends Model { public function callStoredProcedure($procedureName, $parameters = array()) { // Prepare the stored procedure call $stmt = $this->db->prepare("CALL $procedureName(?)"); // Bind the parameters, if any foreach ($parameters as $key => $value) { $stmt->bindParam($key + 1, $value); } // Execute the stored procedure $stmt->execute(); // Retrieve the results, if any $result = $stmt->fetchAll(); // Return the results return $result; } }</code>
在您的Controller 類別(例如,ProcedureController.php)中,您可以存取預存程序方法:
<code class="php">// Controller class (e.g., ProcedureController.php) class ProcedureController extends Controller { public function index() { // Get the parameters from the view $parameters = array(1, 'John Doe'); // Load the Model class $procedureModel = new ProcedureModel(); // Call the stored procedure $result = $procedureModel->callStoredProcedure('GetCustomerInfo', $parameters); // Pass the results to the view $this->view('procedure', array('result' => $result)); } }</code>
在您的View 類別(例如procedure .php)中,您可以顯示結果:
<code class="php">// View class (e.g., procedure.php) <?php foreach ($result as $row): ?> <tr> <td><?php echo $row['customer_id']; ?></td> <td><?php echo $row['customer_name']; ?></td> </tr> <?php endforeach; ?></code>
以上是如何使用MVC架構呼叫PHPMyAdmin中的預存程序?的詳細內容。更多資訊請關注PHP中文網其他相關文章!