<?php
/**
* Created by PhpStorm.
* User: zrj
* Date: 18-6-22
* Time: 上午10:49
*/
declare(strict_types=1);//開啟強(qiáng)類型模式
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();
}
//防止實(shí)例被克隆
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;
}
網(wǎng)站欄目:PHP量級(jí)數(shù)據(jù)遍歷方案:yield生成器
鏈接URL:http://www.rwnh.cn/article26/jisjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站導(dǎo)航、云服務(wù)器、網(wǎng)站設(shè)計(jì)公司、Google、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)