dousong5492 2010-08-11 19:34 采纳率: 100%
浏览 28
已采纳

将mysql资源解压缩到php数组?

My goal is to display the profile of a user. I have this function:

function get_profile($un) {
            if($registerquery = $this->conn->query("SELECT * FROM table WHERE usr = '".$un."' ")){
               return $profile = mysql_fetch_array($registerquery); 
           }
        }

Then the display snippet:

<?php $profile = $mysql->get_profile($un); 

foreach($profile as $key => $value){

     echo "<span>".$key.': '.$value."</span><br />"; 

  }

?>

But I get: "Warning: Invalid argument supplied for foreach() in..."

Help pls???

  • 写回答

7条回答 默认 最新

  • dongsao8279 2010-08-11 20:51
    关注

    You need to see if the result was a success or not

    if (gettype($result) == "boolean") {
        $output = array('success' => ($result ? 1 : 0));
    }
    

    And you need to cycle through it if it's a resource type...

    if (gettype($result) == "resource") {
        if (mysql_num_rows($result) != 0 ) {
            while ($row = mysql_fetch_assoc($result)) {
                $output[] =$row;
            }
        }
    }
    

    I chopped up some real code that does basically everything pretty awful for you because I can't release it, sorry.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效