分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

PHP量级数据遍历方案:yield生成器

发布时间:2023-09-06 02:01责任编辑:赖小花关键词:PHP遍历
<?php/** * Created by PhpStorm. * User: zrj * Date: 18-6-22 * Time: 上午10:49 */declare(strict_types=1);//开启强类型模式class DB{ ???private static $instance = null; ???private static $sth = null; ???private function __construct() ???{ ???????try { ???????????$dsn = "mysql:host=192.168.1.9;dbname=yundou_v3"; ???????????static::$instance = new \PDO($dsn, ‘user‘, ‘password‘); ???????} catch (PDOException $e) { ???????????echo ‘Connection failed: ‘ . $e->getMessage(); ???????????exit(); ???????} ???????static::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); ???} ???public static function getInstance() ???{ ???????if (!(static::$instance instanceof \PDO)) { ???????????new static(); ???????} ???????return static::$instance; ???} ???public static function excuteQuery(string $sql) ???{ ???????static::$sth = static::$instance->prepare($sql); ???????static::$sth->execute(); ???} ???//防止实例被克隆 ???private function __clone() ???{ ???} ???public static function cursor() ???{ ???????while ($row = static::$sth->fetch(\PDO::FETCH_ASSOC)) { ???????????yield $row; ???????} ???}}DB::getInstance();DB::excuteQuery("select * from bill_info");foreach (DB::cursor() as $info) { ???echo $info[‘id‘] . PHP_EOL;}

PHP量级数据遍历方案:yield生成器

原文地址:http://blog.51cto.com/phpme/2131743

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved