Call to a member function fetch() on a non-object错误

Monkey_Online 2012-08-09 10:19:35
try{
$dsn = "mysql:host=localhost;dbname=db_upan";
$db = new PDO($dsn,"root","");
$sql = "select * form tb_uptype";
$statement = $db->query($sql);
while($result = $statement->fetch(PDO::FETCH_ASSOC )){
echo $result['genrename'];
}
$db = null;
}catch (PDOException $e){
echo "发生错误。";
}
代码如上: 运行的时候提示错误Fatal error: Call to a member function fetch() on a non-object in C:\xampp\htdocs\upan\test.php on line 7;
请问这是什么原因啊? 坐等。。。
...全文
8577 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Monkey_Online 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

Call to a member function fetch() on a non-object
用对象调用的方式调用了函数fetch 但是你给的 $statement php认为他不是一个对象 不可用$statement->fetch() 这种方式。
那么你就var_dump($statement);
看一下你从数据库取得数据对不对。
根据这个你就能知道错误出在哪儿了。
[/Quote]
呵呵,谢谢回复,问题解决了。。。
Monkey_Online 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

$sql = "select * form tb_uptype";
应为
$sql = "select * from tb_uptype";

sql 指令错了,自然后继代码也要出错
[/Quote]
谢谢,就是这个错误,太粗心了。。。
Monkey_Online 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

$sql = "select * from tb_uptype";
[/Quote]
谢谢了,的确是这个错误,刚开始学,很多地方太粗心了,以后一定注意。。。
蓝色的雨哎 2012-08-09
  • 打赏
  • 举报
回复
Call to a member function fetch() on a non-object
用对象调用的方式调用了函数fetch 但是你给的 $statement php认为他不是一个对象 不可用$statement->fetch() 这种方式。
那么你就var_dump($statement);
看一下你从数据库取得数据对不对。
根据这个你就能知道错误出在哪儿了。
xuzuning 2012-08-09
  • 打赏
  • 举报
回复
$sql = "select * form tb_uptype";
应为
$sql = "select * from tb_uptype";

sql 指令错了,自然后继代码也要出错
一起混吧 2012-08-09
  • 打赏
  • 举报
回复
$sql = "select * from tb_uptype";
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,886

社区成员

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

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