qq_27107813
2016-05-08 14:28
采纳率: 0%
浏览 1.5k

在php中使用sql游标select将数据存入数组中

#这个用于处理游标到达最后一行的情况
DECLARE s int default 0;

#声明游标cursor_name(cursor_name是个多行结果集)
DECLARE cursor_name CURSOR FOR select NNF_ID,NNF_NAME,NNF_REALNAME,NNF_NOTICE_ID from t_notice_file;

#设置一个终止标记
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;

#打开游标
OPEN cursor_name;

#获取游标当前指针的记录,读取一行数据并传给变量a,b
fetch cursor_name into a,b,c,d;

#开始循环,判断是否游标已经到达了最后作为循环条件
while s <> 1 do

insert into t_ehdnotice_file(id,name,original_name,create_time,ehdnotice_id,path) values(a,b,c,now(),d,'2014-04\15');

#读取下一行的数据
fetch cursor_name into a,b,c,d;
end while;

#关闭游标
CLOSE cursor_name ;

#语句执行结束
END

网上找到这段SQL使用游标的代码,不知道如何将读取的数据存入php数组中。
请大神赐教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • havedream_one 2016-05-09 00:42
    BEGIN
        DECLARE var1 INT DEFAULT 0;
        DECLARE done INT DEFAULT 0;
        DECLARE cardsortID INT DEFAULT 0;
     DECLARE alart  INT DEFAULT 0;
    
        DECLARE cur CURSOR FOR (SELECT sortID, alarmCount  FROM monitoring WHERE hide=1 ORDER BY sortID ASC);
    
    
        open cur;
        FETCH cur INTO cardsortID, alart;
        WHILE done <> 1 DO 
          SELECT cardName,cardsortID,count(*) AS leftCount,alart as cardCount FROM cards 
             WHERE endDate>now() AND cardStatus=3 AND operator=cardsortID;
           FETCH cur INTO cardsortID, alart;
        END WHILE;
    CLOSE cur;
    END
     $dblink=mysqli_connect('localhost','root','root')
                or die("can not connect to mysql");
      mysqli_select_db($dblink,$db)
          or die("can not select salecards");
    
        //$query=mysqli_multi_query($dblink,"CALL proc_AlarmCount()")or die("Invalid query: " . mysqli_error());
    $query=mysqli_query($dblink,"CALL proc_AlarmCount()")or die("Invalid query: " . mysqli_error());
        //var_dump($query);
    
    if ($query) {
          $i=1;
     mysqli_next_result($dblink);
      do {  
           if ($result = mysqli_store_result($dblink)) {
                while ($row = mysqli_fetch_assoc($result)) {
    
                    var_dump($row);
                }
                mysqli_free_result($result);
            }
    printf("
    $i-----------------
    ");
    $i++;
        } while (mysqli_next_result($dblink));    
    
    
    }
    
    //$row=mysqli_fetch_row($result);
    //var_dump($row);
    
    mysqli_close($dblink);
    
    点赞 打赏 评论

相关推荐 更多相似问题