如题所述
mysql_queryå½æ°æ¥è¯¢çæ¹å¼æ¯æ¥è¯¢åºå
¨é¨ç»æåç¼åå°å
åä¸ï¼è¿æ ·å°±ä¼åºç°è¶
å
åçç°è±¡ï¼ä½¿ç¨å¦å¤ä¸ä¸ªå½æ°mysql_unbuffered_queryå¯ä»¥è§£å³è¿ä¸ªé®é¢ï¼mysql_unbuffered_queryä¸ä¼ç¼åç»æéï¼èæ¯æ¥è¯¢åºæ¥æ°æ®åç«é©¬å¯¹ç»æéè¿è¡æä½ï¼ä¹å°±æ¯ä¾¿æ¥è¯¢è¾¹è¿åï¼è¿æ ·å°±ä¸ä¼åºç°è¶
åºå
åçç°è±¡ï¼ä½æ¯ä½¿ç¨mysql_unbuffered_queryçæ¯æ¶åä¸è½ä½¿ç¨ mysql_num_rows() å mysql_data_seek()ã并ä¸å MySQL åéä¸æ¡æ°ç SQL æ¥è¯¢ä¹åï¼å¿
é¡»æåææææªç¼åç SQL æ¥è¯¢æ产ççç»æè¡ãä¾å¦ï¼
使ç¨ç¼åç»æéç代ç ï¼
function selecttest()
{
try {
$pdo = new PDO("mysql:host=localhost;dbname=test", 'root', '123456');
// ä¸ä½¿ç¨ç¼åç»æéæ¹å¼
// $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$sth = $pdo->prepare('select * from test');
$sth->execute();
echo 'æåå ç¨å å大å°ï¼' . memory_get_usage() . "\n";
$i = 0;
while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
$i += 1;
if ($i > 10) {
break;
}
sleep(1);
print_r($result);
echo 'å ç¨å å大å°ï¼' . memory_get_usage() . "\n";
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
æ§è¡æ¶å°ä¼æ¥è¶ åºå åçé误ï¼
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 204800000 bytes) in E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php on line 56
Call Stack:
0.0005 135392 1. {main}() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:0
0.0005 135568 2. test->selecttest() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:85
0.0050 142528 3. PDOStatement->execute() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:56
å°ä¸é¢ä»£ç ä¸ç$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);ä¸è¡ç注éå»æåå°ä¸å¨ç¼åç»æéï¼è¿æ¶è¿è¡è¯¥å½æ°çç»æå¦ä¸ï¼
æåå ç¨å å大å°ï¼144808
Array
(
[id] => 1
[a] => v
[b] => w
[c] => i
)
å ç¨å å大å°ï¼145544
Array
(
[id] => 2
[a] => b
[b] => l
[c] => q
)
å ç¨å å大å°ï¼145544
Array
(
[id] => 3
[a] => m
[b] => p
[c] => h
)
å ç¨å å大å°ï¼145536
Array
(
[id] => 4
[a] => j
[b] => i
[c] => b
)
å ç¨å å大å°ï¼145536
å¯ä»¥çå°ï¼è¿æ¶è¿åä¸æ¡æ°æ®å åå ç¨é常çå°ï¼ä¹å°±700å¤åèï¼è¿æ ·å°±ä¸ä¼åºç°è¶ åºå åçé误äºã
使ç¨ç¼åç»æéç代ç ï¼
function selecttest()
{
try {
$pdo = new PDO("mysql:host=localhost;dbname=test", 'root', '123456');
// ä¸ä½¿ç¨ç¼åç»æéæ¹å¼
// $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$sth = $pdo->prepare('select * from test');
$sth->execute();
echo 'æåå ç¨å å大å°ï¼' . memory_get_usage() . "\n";
$i = 0;
while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
$i += 1;
if ($i > 10) {
break;
}
sleep(1);
print_r($result);
echo 'å ç¨å å大å°ï¼' . memory_get_usage() . "\n";
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
æ§è¡æ¶å°ä¼æ¥è¶ åºå åçé误ï¼
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 204800000 bytes) in E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php on line 56
Call Stack:
0.0005 135392 1. {main}() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:0
0.0005 135568 2. test->selecttest() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:85
0.0050 142528 3. PDOStatement->execute() E:\ProgramDevelopment\RuntimeEnvironment\xampp\htdocs\test\test.php:56
å°ä¸é¢ä»£ç ä¸ç$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);ä¸è¡ç注éå»æåå°ä¸å¨ç¼åç»æéï¼è¿æ¶è¿è¡è¯¥å½æ°çç»æå¦ä¸ï¼
æåå ç¨å å大å°ï¼144808
Array
(
[id] => 1
[a] => v
[b] => w
[c] => i
)
å ç¨å å大å°ï¼145544
Array
(
[id] => 2
[a] => b
[b] => l
[c] => q
)
å ç¨å å大å°ï¼145544
Array
(
[id] => 3
[a] => m
[b] => p
[c] => h
)
å ç¨å å大å°ï¼145536
Array
(
[id] => 4
[a] => j
[b] => i
[c] => b
)
å ç¨å å大å°ï¼145536
å¯ä»¥çå°ï¼è¿æ¶è¿åä¸æ¡æ°æ®å åå ç¨é常çå°ï¼ä¹å°±700å¤åèï¼è¿æ ·å°±ä¸ä¼åºç°è¶ åºå åçé误äºã
温馨提示:答案为网友推荐,仅供参考