问题描述
- 在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 doinsert into t_ehdnotice_file(id,name,original_name,create_time,ehdnotice_id,path) values(a,b,c,now(),d,'2014-0415');
#读取下一行的数据
fetch cursor_name into a,b,c,d;
end while;#关闭游标
CLOSE cursor_name ;#语句执行结束
END网上找到这段SQL使用游标的代码,不知道如何将读取的数据存入php数组中。
请大神赐教
解决方案
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);
时间: 2024-10-31 22:27:35