这里php读取mysql结果集为什么不能用存储过程

aquastar 2010-06-21 11:08:16
首先创建一个mysql存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `News_getAll`()
select * from news;


接着我在php操作类里面写了一个方法
mysql.class.php:

function News_getAll() {
$allnews = array ();
$i = 0;
$result = mysql_query("select * from news");
while ($row = mysql_fetch_row($result)) {
$allnews[$i] = $row;
$i++;
}
return $allnews;
}


然后再用smarty模板输出
index.php:

<?php
include "smarty_inc.php";
include "mysql.class.php";

$db = new mysql('localhost', 'root', '', 'tianmei', 'GBK');
$allnews=$db->News_getAll();


$smarty->assign('allnews', $allnews);
$smarty->display('index.html');
?>


我的模板页是
index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
</head>

<body>

{foreach item=v from=$allnews }
{$v[0]}
{$v[1]}
{$v[2]}
{$v[3]}<br>
{/foreach}


</body>
</html>


结果是可以正常显示,但是如果我把操作类的
$result = mysql_query("select * from news");

换成
$result = mysql_query("call News_getAll()");

就报错 为 mysql_fetch_row(): supplied argument is not a valid MySQL result resource

如果有人知道可请教我这里如何调用存储过程
...全文
212 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoguangyi 2010-11-04
  • 打赏
  • 举报
回复
没写错,你的数据库连接方式不支持mysql存储过程返回结果集,需要改写:
mysql_connect("服务器","用户名","密码",1,131072)
aquastar 2010-06-21
  • 打赏
  • 举报
回复
奇怪的是,我同样在操作类里面写

function News_add($title, $cont) {
mysql_query("call News_add('" . $title . "','" . $cont . "')");
}

function News_deleteById($id){
mysql_query("call News_deleteById(".$id.")");
}

function News_modify($title,$cont,$id){
mysql_query("call News_modify('".$title."','".$cont."',".$id.")");
}

等方法调用存储过程,在php页用

$db = new mysql('localhost', 'root', '', 'tianmei', 'GBK');

$db->News_deleteById(18);
$db->News_News_modify('XXX','XXX',18);
$db->News_News_add('XXX','XXXX');


都正常进行操作,不解了
xiaocai520 2010-06-21
  • 打赏
  • 举报
回复
http://www.lihuasoft.net/article/show.php?id=2806
看看这个吧,很好解决滴~~~
$res=mysql_query("call aa(@a)",$dblink);
$res=mysql_query("select @a",$dblink);
$row=mysql_fetch_row($res);
一共是三步
myhope88 2010-06-21
  • 打赏
  • 举报
回复
mysql肯定可以调用存储过程的,网上搜一下示例代码吧
aquastar 2010-06-21
  • 打赏
  • 举报
回复
再顶一下,发现查询操作用函数和存储过程都不行,但增删改都正常,我这哪里写错了吗?

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧