Call to a member function fetch_array() on a non-object错误,求助!

subnet 2006-10-20 09:14:41
我有两个存储过程:
---------------------------------------
1,计算总数:

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_select_count` $$
CREATE PROCEDURE `sp_select_count`(IN sp_likeSql TEXT,OUT recordCount INT)
BEGIN

SET @sqlStr = CONCAT('select count(id) into @recordCount from ',sp_likeSql);
PREPARE sqlstmt FROM @sqlStr;
EXECUTE sqlstmt;
SET recordCount = @recordCount;
DEALLOCATE PREPARE sqlstmt;

END $$

DELIMITER ;

---------------------------------------
2,返回记录集

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_select_result` $$
CREATE PROCEDURE `sp_select_result`(IN likeSql TEXT)
BEGIN

SET @sqlStr = CONCAT('select * from ',likeSql);
PREPARE sqlstmt FROM @sqlStr;
EXECUTE sqlstmt;
DEALLOCATE PREPARE sqlstmt;

END $$

DELIMITER ;

问题是当我执行一次以上两个文件时,是正确的,结果输出也没问题,但当我在一个文件的前后,分别执行两次以上存储过程时,在第二次执行时,就提示Call to a member function fetch_array() on a non-object.

代码是:

执行第一个存储过程的代码,计算总和:
$likeSql = 'tbl_name WHERE name = "'.$getName.'"';
$db->query("CALL tets.sp_select_count('".$likeSql."',@recordCount)");
$result = $db->query("select @recordCount");
$row = $result->fetch_array();
$recordCount = $row['@recordCount'];

执行第二个存储过程的代码,返回记录集,用于在页面上循环输出:
$result = $db->query("CALL test.sp_select_result('".$limitSql."')");

如果只执行以上代码一次,是没问题的,如果分别执行两次,第二次就会出现Call to a member function fetch_array() on a non-object,我试了下,第二次执行时 $result 并不是返回一个对像,问题应该在这里吧,但为什么执行一次没问题,执行二次却有错误?难道存储过程执行完一次后要关闭什么东西吗?
...全文
1251 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
subnet 2006-11-29
  • 打赏
  • 举报
回复
问题还未解决,也许是存储过程的问题,先结帖了
gu1dai 2006-10-25
  • 打赏
  • 举报
回复
$db->query("CALL tets.sp_select_count('".$likeSql."',@recordCount)");
$db->query("CALL test.sp_select_result('".$limitSql."')");

这里参数不同,或许是这个原因,我没研究过mysql的存储过程。
helloyou0 2006-10-21
  • 打赏
  • 举报
回复
查看一下$db在两次执行之间有否被无意改动过.
subnet 2006-10-20
  • 打赏
  • 举报
回复
释放$db了,还是没有,试过用新的变量也没用.

如果再创建一个数据库连接对像$db1,然后一组用$db1,一组用$db,才能解决,那这样两个连接对像就没效率了,怎么办啊???
Jsu02dk 2006-10-20
  • 打赏
  • 举报
回复
$row = $result->fetch_array();


$row = $db->fetch_array($result);

myvicy 2006-10-20
  • 打赏
  • 举报
回复
释放$db然后在用试试,或者就每次用新的变量。
subnet 2006-10-20
  • 打赏
  • 举报
回复
如果我创建两个连接对像,一个叫$db,一个叫$db1,再执行二遍上面二个存储过程,就正确了.这代表什么啊?是不是如果用一个$db去执行上述操作,执行完应该释放$db?我用了unset没用啊,谢谢再帮下我吧
subnet 2006-10-20
  • 打赏
  • 举报
回复
tbl_hobby里面有数据,$db是一个连接对像,用5.0里面的mysqli
颓废的老猫 2006-10-20
  • 打赏
  • 举报
回复
你的$db是一个数据库连接对象吗?

xuzuning 2006-10-20
  • 打赏
  • 举报
回复
同一个存储过程执行两次就失败?可能是你的存储过程有问题

$result = $db->query($sql);
当执行失败的时候就不能返回哈法的对象,自然也就没有fetch_array()了
这又从另一个方面说明你的数据库类设计有问题
颓废的老猫 2006-10-20
  • 打赏
  • 举报
回复
看是你的tbl_hobby表里有没有数据

或者你直接写sql语句执行下看会不会有问题
subnet 2006-10-20
  • 打赏
  • 举报
回复
忘说重要的一点了,第一次执行那两个存储过程是在函数内,函数开头用了global $db;,我发现如果再创建一个新的$db1,然后在函数里面用$db1去执行,第二次执行两个存储过程时用$db,这样就正确了,不知道这是为什么啊,我不想创建两个$db类,有什么好的方法吗?
subnet 2006-10-20
  • 打赏
  • 举报
回复
第一组执行记算总数和返回记录集用的都是tbl_name表
第二组执行记算总数和返回记录集用的都是tbl_hobby表

如果只执行其中任意一组,就会正确,但若一起执行,第二组就会失败.
比如存储过程a,还有一个是存储过程b,如果a执行完再执行b,是正确的,如果a执行完再执行b,再执行a,再执行b,那第二次的a和b就会错误.
Gdj 2006-10-20
  • 打赏
  • 举报
回复
你是不是在函数里调用了。$db必须建成global变量

$db=new db()....

function 调用1()
{
global $db;
$db->query(...
}

function 调用2()
{
global $db;
$db->query(...
}
Twitter Digg Facebook Del.icio.us Reddit Stumbleupon Newsvine Technorati Mr. Wong Yahoo! Google Windows Live Send as Email Add to your CodeProject bookmarks Discuss this article 85 Print Article Database » Database » Other databasesLicence CPOL First Posted 19 Jan 2012 Views 24,219 Downloads 992 Bookmarked 74 times RaptorDB - The Key Value Store V2 By Mehdi Gholam | 8 Mar 2012 | Unedited contribution C#.NETDBABeginnerIntermediateAdvanceddatabase Even faster Key/Value store nosql embedded database engine utilizing the new MGIndex data structure with MurMur2 Hashing and WAH Bitmap indexes for duplicates. See Also More like this More by this author Article Browse Code Stats Revisions (8) Alternatives 4.95 (56 votes) 1 2 3 4 5 4.95/5 - 56 votes μ 4.95, σa 1.05 [?] Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions. Add your own alternative version Introduction What is RaptorDB? Features Why another data structure? The problem with a b+tree Requirements of a good index structure The MGIndex Page Splits Interesting side effects of MGIndex The road not taken / the road taken and doubled back! Performance Tests Comparing B+tree and MGIndex Really big data sets! Index parameter tuning Performance Tests - v2.3 Using the Code Differences to v1 Using RaptorDBString and RaptorDBGuid Global parameters RaptorDB interface Non-clean shutdowns Removing Keys Unit tests File Formats File Format : *.mgdat File Format : *.mgbmp File Format : *.mgidx File Format : *.mgbmr , *.mgrec History Download RaptorDB_v2.0.zip - 38.7 KB Download RaptorDB_v2.1.zip - 39 KB Download RaptorDB_v2.2.zip - 39 KB Download RaptorDB_v2.3.zip - 39.6 KB D

21,887

社区成员

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

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