用PHP呼叫MySQL儲存過程
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: {13: $rs = $dblink->query(”select @num”);14: $row = $rs->fetch_array();15: echo $row[’@num’], ‘<br>’;16: 17: $rs = $dblink->query(”select @x”);18: $row = $rs->fetch_array();19: echo $row[’@x’];20: 21: mysqli_free_result($rs);22: }23: else24: echo ‘error…’;25: mysqli_close($dblink);26: 27: /*28: – Procedure “sp_test0″ DDL29: CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test0`(OUT num INT, OUT x VARCHAR(16), IN n INT)30: BEGIN31: 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 = ‘XXX’;39: END;40: */41: ?>42:
二、返回結果集:
- : <?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: {13: while( $row = $result->fetch_array())14: {15: echo ($row[’nId’]. “–” . $row[’sTopic2′] . “<br>”);16: }17: mysqli_free_result($result);18: }19: else20: echo ‘error…’;21: mysqli_close($dblink);22: 23: /*24: – Procedure “sp_test1″ DDL25: CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test1`()26: BEGIN27: SELECT * FROM open_news WHERE nId<40;28: END;29: */30: ?>
三、返回多個結果集:
- : <?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: {12: print(’Can not connect to MySQL server’);13: exit;14: }15: else16: print(’?????? MySQL ????????<br>’);17: 18: $dblink->query(’SET NAMES UTF8′);19: $rows = array();20: if($dblink->real_query(”CALL sp_test2()”))21: {22: do23: {24: if($result = $dblink->store_result())25: {26: while ($row = $result->fetch_assoc())27: {28: array_push($rows, $row);29: }30: $result->close();31: }32: }33: while($dblink->next_result());34: }35: else36: echo ‘error…’;37: 38: $dblink->close();39: 40: print_r($rows);41: /*42: – Procedure “sp_test2″ DDL43: CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test2`()44: BEGIN45: SELECT nId,sTopic2 FROM open_news LIMIT 0, 5;46: SELECT count(nId) AS counter FROM open_news;47: END;48: */49: ?>
本文轉自網眼51CTO部落格,原文連結:http://blog.51cto.com/itwatch/286529,如需轉載請自行聯絡原作者
相關文章
- PHP呼叫MYSQL儲存過程例項PHPMySql儲存過程
- php呼叫mssql儲存過程PHPSQL儲存過程
- mysql如何呼叫儲存過程MySql儲存過程
- c / c + + 呼叫mysql儲存過程MySql儲存過程
- PHP實現多儲存過程呼叫PHP儲存過程
- php呼叫mysql儲存過程和函式的方法(轉)PHPMySql儲存過程函式
- 用java呼叫oracle儲存過程JavaOracle儲存過程
- MySQL儲存過程語句及呼叫MySql儲存過程
- mysql 儲存過程,以及mybatis如何呼叫MySql儲存過程MyBatis
- mysql-定時呼叫儲存過程MySql儲存過程
- 呼叫儲存過程儲存過程
- MySQL儲存過程詳解 mysql 儲存過程MySql儲存過程
- mysql多次呼叫儲存過程的問題MySql儲存過程
- Winform呼叫儲存過程ORM儲存過程
- perl呼叫儲存過程儲存過程
- jdbc呼叫儲存過程JDBC儲存過程
- SQL 儲存過程裡呼叫另一個儲存過程SQL儲存過程
- MySQL儲存過程詳解 mysql 儲存過程linkMySql儲存過程
- 用java呼叫oracle儲存過程總結JavaOracle儲存過程
- mysql 儲存過程MySql儲存過程
- linux呼叫儲存過程Linux儲存過程
- PB中呼叫儲存過程儲存過程
- java 呼叫oracle 儲存過程JavaOracle儲存過程
- java中呼叫儲存過程Java儲存過程
- 在儲存過程A中呼叫儲存過程B的結果儲存過程
- mysql儲存過程及c#呼叫標準版MySql儲存過程C#
- MySql儲存過程—2、第一個MySql儲存過程的建立MySql儲存過程
- mysql儲存過程整理MySql儲存過程
- MySQL之儲存過程MySql儲存過程
- [MYSQL -23儲存過程]MySql儲存過程
- MYSQL儲存過程管理MySql儲存過程
- mysql儲存過程例子MySql儲存過程
- mysql的儲存過程MySql儲存過程
- MySQL---------儲存過程MySql儲存過程
- jsp中呼叫儲存過程JS儲存過程
- Spring mybatis 呼叫儲存過程SpringMyBatis儲存過程
- C#呼叫 oracle儲存過程C#Oracle儲存過程
- C#呼叫Oracle儲存過程C#Oracle儲存過程