查询当前数据库有哪些存储过程:
创建一个简单的存储过程
5 存储过程创建完毕,看怎么调用它
显示结果
删除存储过程:
存储过程实例:
PHP调用存储过程:
返回单个数据:
1.1:创建MySQL存储过程:
1.2:PHP调用:
返回多个数据:
2.1:创建MySQL存储过程:
2.2:PHP调用:
P.S.:以上代码执行通过环境 PHP 5.X.X + MySQL 5.X.X
下面就可以正常使用了,以下是例子程序:
mysql>show procedure status where Db='test'
创建一个简单的存储过程
mysql>create procedure hi() select 'hello';
5 存储过程创建完毕,看怎么调用它
mysql>call hi();
显示结果
mysql> call hi();
+-------+
| hello |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
+-------+
| hello |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
删除存储过程:
DROP PROCEDURE hi;
存储过程实例:
DELIMITER ||
CREATE PROCEDURE showdb( v_offset int, v_pagesize int )
BEGIN
SET @stmt = concat("SELECT * FROM `comment` ORDER BY id DESC LIMIT ",v_offset,",",v_pagesize);
PREPARE s1 FROM @stmt;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END||
DELIMITER ;
CREATE PROCEDURE showdb( v_offset int, v_pagesize int )
BEGIN
SET @stmt = concat("SELECT * FROM `comment` ORDER BY id DESC LIMIT ",v_offset,",",v_pagesize);
PREPARE s1 FROM @stmt;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END||
DELIMITER ;
PHP调用存储过程:
返回单个数据:
1.1:创建MySQL存储过程:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`proc_test` $$
CREATE PROCEDURE `test`.`proc_test` (out a int)
BEGIN
select count(*) into a from tblname;
END $$
DELIMITER ;
DROP PROCEDURE IF EXISTS `test`.`proc_test` $$
CREATE PROCEDURE `test`.`proc_test` (out a int)
BEGIN
select count(*) into a from tblname;
END $$
DELIMITER ;
1.2:PHP调用:
$db->query("CALL test.proc_test(@a)");
$res = $db->query("select @a");
$row = $res->fetch_array();
echo $row['@a'];
$res = $db->query("select @a");
$row = $res->fetch_array();
echo $row['@a'];
返回多个数据:
2.1:创建MySQL存储过程:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`proc_test` $$
CREATE PROCEDURE `test`.`proc_test` ()
BEGIN
select * from tbl_name;
END $$
DELIMITER ;
DROP PROCEDURE IF EXISTS `test`.`proc_test` $$
CREATE PROCEDURE `test`.`proc_test` ()
BEGIN
select * from tbl_name;
END $$
DELIMITER ;
2.2:PHP调用:
$res=$db->query("CALL test.proc_test()");
while ($arr=$res->fetch_array())
{
echo $arr["Field"] ."<br/>";
}
while ($arr=$res->fetch_array())
{
echo $arr["Field"] ."<br/>";
}
P.S.:以上代码执行通过环境 PHP 5.X.X + MySQL 5.X.X
1 define('CLIENT_MULTI_RESULTS', 131072);
2
3 $link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());
2
3 $link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());
下面就可以正常使用了,以下是例子程序:
1 <?php
2 define('CLIENT_MULTI_RESULTS', 131072);
3
4 $link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());
5 mysql_select_db("vs") or die("Could not select database");
6 ?>
7
8 <?php
9 $result = mysql_query("call get_news_from_class_id(2)") or die("Query failed:" .mysql_error());
10 while($row = mysql_fetch_array($result, MYSQL_ASSOC))
11 {
12 $line = '<tr><td><a target = _blank href=\''.$row["url"].'\'>'.$row["title"].'('.$row["page_time"].')'.'</a></td></tr>';
14 echo $line;
15 printf("\n");
16
17 }
18 mysql_free_result($result);
19 ?>
20
21 <?php
22 mysql_close($link);
23 ?>
2 define('CLIENT_MULTI_RESULTS', 131072);
3
4 $link = mysql_connect("127.0.0.1", "root", "",1,CLIENT_MULTI_RESULTS) or die("Could not connect: ".mysql_error());
5 mysql_select_db("vs") or die("Could not select database");
6 ?>
7
8 <?php
9 $result = mysql_query("call get_news_from_class_id(2)") or die("Query failed:" .mysql_error());
10 while($row = mysql_fetch_array($result, MYSQL_ASSOC))
11 {
12 $line = '<tr><td><a target = _blank href=\''.$row["url"].'\'>'.$row["title"].'('.$row["page_time"].')'.'</a></td></tr>';
14 echo $line;
15 printf("\n");
16
17 }
18 mysql_free_result($result);
19 ?>
20
21 <?php
22 mysql_close($link);
23 ?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1318/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2008-10-22 14:41
评论列表
2009-4-3 11:23 | fff
查询代码不对吧
分页: 1/1 1