mysql 5.0 以上支援儲存過程。
php 5.0 以上的 mysqli 系列函式可以支援操作 mysql 的儲存過程。
以下是一些簡單的儲存過程和用 php 呼叫的示例。
一、返回單個資料:
1:<?php
2: header("content-type:text/html;charset=utf-8");
3:
4: $host = "localhost";
5: $user = "root";
6: $password = "mypassword";
7: $db = "test_store_proc";
8: $dblink = mysqli_connect($host, $user, $password, $db) or die("can't connect to mysql");
9:
10: $dblink->query('set names utf8');
11: if ($result = $dblink->query("call sp_test0(@num, @x, 123)"))
12:
23: else
24: echo 'error...';
25: mysqli_close($dblink);
26:
27: /*
28: -- procedure "sp_test0" ddl
29: create definer=`root`@`localhost` procedure `sp_test0`(out num int, out x varchar(16), in n int)
30: begin
31: declare nouse int;
32: declare tmp int;
33:
34: select nid into nouse from open_news where nid=39;
35: select count(*) into tmp from open_news;
36: set num = tmp;
37:
38: set x = '***';
39: end;
40: */
41: ?>
42:
二、返回結果集:
1:<?php
2: header("content-type:text/html;charset=utf-8");
3:
4: $host = "localhost";
5: $user = "root";
6: $password = "mypassword";
7: $db = "test_store_proc";
8: $dblink = mysqli_connect($host, $user, $password, $db) or die("can't connect to mysql");
9:
10: $dblink->query('set names utf8');
11: if ($result = $dblink->query("call sp_test1()"))
12:
17: mysqli_free_result($result);
18: }
19: else
20: echo 'error...';
21: mysqli_close($dblink);
22:
23: /*
24: -- procedure "sp_test1" ddl
25: create definer=`root`@`localhost` procedure `sp_test1`()
26: begin
27: select * from open_news where nid<40;
28: end;
29: */
30: ?>
31:
三、返回多個結果集:
1:<?php
2: header("content-type:text/html;charset=utf-8");
3:
4: $host = "localhost";
5: $user = "root";
6: $password = "mypassword";
7: $db = "test_store_proc";
8:
9: $dblink = new mysqli($host, $user, $password, $db);
10: if (mysqli_connect_errno())
11:
15: else
16: print('?????? mysql ????????');
17:
18: $dblink->query('set names utf8');
19: $rows = array();
20: if($dblink->real_query("call sp_test2()"))
21:
30: $result->close();
31: }
32: }
33: while($dblink->next_result());
34: }
35: else
36: echo 'error...';
37:
38: $dblink->close();
39:
40: print_r($rows);
41: /*
42: -- procedure "sp_test2" ddl
43: create definer=`root`@`localhost` procedure `sp_test2`()
44: begin
45: select nid,stopic2 from open_news limit 0, 5;
46: select count(nid) as counter from open_news;
47: end;
48: */
49: ?>
50:
「十萬個為什麼」電腦學習網:http://www.why100000.com
用 PHP 呼叫 MySQL 儲存過程
mysql 5.0 以上支援儲存過程。php 5.0 以上的 mysqli 系列函式可以支援操作 mysql 的儲存過程。以下是一些簡單的儲存過程和用 php 呼叫的示例。一 返回單個資料 1 host localhost 5 user root 6 password mypassword 7 db...
php呼叫mysql 儲存過程
php可以通過查詢語句 call procedurename 來呼叫mysql的儲存過程。但不能使用mysql 相關函式得用mysqli 相關函式。建立儲存過程 use test create procedure sta select from ta connstr mysqli connect l...
MySQL呼叫儲存過程
使用儲存過程,可以使程式執行效率更高,安全性更好,增強程式的可重用性和維護性 儲存過程有多種呼叫方法 儲存過程必須使用call語句呼叫,並且,儲存過程和資料庫相關,如果,要執行其他資料庫中的儲存過程,需要指定資料庫名稱 語法格式 call sp name parameter sp name,為儲存過...