首先创建一个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 如果有人知道可请教我这里如何调用存储过程
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 如果有人知道可请教我这里如何调用存储过程
看看这个吧,很好解决滴~~~
$res=mysql_query("call aa(@a)",$dblink);
$res=mysql_query("select @a",$dblink);
$row=mysql_fetch_row($res);
一共是三步
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');
都正常进行操作,不解了
mysql_connect("服务器","用户名","密码",1,131072)