首页 > 开发 > PHP > 正文

mysql_fetch_array 与 mysql_fetch_object函数与用法

2024-05-04 21:59:12
字体:
来源:转载
供稿:网友

总结:mysql_fetch_object 把记录作来一个对象来处理,像我们用类时就要用->访问,mysql_fetch_array 把记录保存到一个数据所以可以用$rs['下标名'] 或$rs[0]数组编号.

PHP实例代码如下:

  1. $conn=mysql_connect("127.0.0.1","root","root"); 
  2.  mysql_select_db("ip"); 
  3.  $sql="select * from adminblog  "
  4.  $result=mysql_query($sql,$conn); 
  5.       
  6.  while($rs=mysql_fetch_array($result)) 
  7.         { 
  8.    echo $rs->username; 
  9.    echo $rs->password; 
  10.   } 
  11. //运行代码提示Notice: Trying to get property of non-object 好了,我们现在修改一下 
  12.  
  13. while($rs=mysql_fetch_array($result)) 
  14.         { 
  15.    echo $rs['username']; 
  16.    echo $rs['password']; 
  17.   } 
  18.  
  19.  //输出结果: adsense 5498bef14143cd98627fb0560cb5ded6 
  20. //现在来看一个mysql_fetch_object的实例 
  21.   
  22. while($rs=mysql_fetch_object($result)) 
  23.         { 
  24.    echo $rs['username']; 
  25.     
  26.   }  
  27.  
  28. //运行后出来 Fatal error: Cannot use object of type stdClass as array in 说这不是一个数组 
  29.  
  30. while($rs=mysql_fetch_object($result)) 
  31.         { 
  32.    echo $rs->username; 
  33.    //开源代码Vevb.com 
  34.   } 
  35.  
  36. //输出结果为 adsense

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表