The connection has timed out

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

buay
PHP Super Member
PHP Super Member
โพสต์: 250
ลงทะเบียนเมื่อ: 02/06/2014 9:55 am

The connection has timed out

โพสต์ที่ยังไม่ได้อ่าน โดย buay »

จาก code ได้แบ่งช่วงการ insert ข้อมูลทีละ500 แล้วเกิดปัญหา The connection has timed out แก้ยังไงคะ (ข้อมูลในการ insert มีประมาณ 9 หมื่นกว่าค่ะ)

โค้ด: เลือกทั้งหมด

$cn = odbc_connect("pro_mdb2", "", "") or die("ติดต่อฐานข้อมูลไม่ได้");
$rs_count = odbc_exec($cn, 'SELECT count(*) FROM WDATA ') or die("รัน SQL ไม่ผ่าน");
$count =  odbc_result($rs_count, 1);
$limit = 500;
$loop = ceil($count/$limit);
//echo "<br>total No. of rows: ".  $loop;
for($i=110;$i<$loop;$i++){   
        $sql = 'INSERT INTO WDATA (field1,field2,...) VALUES ';
    $arr = array();    
    $items = 0;
    
    $rs = odbc_exec($cn, 'SELECT * FROM WDATA ') or die("รัน SQL ไม่ผ่าน");
    while( odbc_fetch_row($rs) ) {  
        $items++; 
        if(($limit * $i) <= $items && ($limit * $i) + $limit >= $items) {
            $truck = iconv( 'TIS-620', 'UTF-8', odbc_result($rs, 2));  
            $arr[]  = " ('".odbc_result($rs, 1)."','".$truck."'
                    ,'".odbc_result($rs, 3)."','".odbc_result($rs, 4)."','".odbc_result($rs, 5)."','".odbc_result($rs, 6)."'
                    ,'".odbc_result($rs, 7)."','".odbc_result($rs, 8)."','".odbc_result($rs, 9)."','".odbc_result($rs, 10)."'
                    ,'".odbc_result($rs, 11)."','".odbc_result($rs, 12)."','".odbc_result($rs, 13)."','".odbc_result($rs, 14)."'
                    ,'".odbc_result($rs, 15)."','".odbc_result($rs, 16)."','".odbc_result($rs, 17)."','".odbc_result($rs, 18)."'
                    ,'".odbc_result($rs, 19)."','".(odbc_result($rs, 20) == "" ? "0000-00-00" : odbc_result($rs, 20))."','".(odbc_result($rs, 21) == "" ? "00:00:00" : odbc_result($rs, 21))."','".odbc_result($rs, 22)."'
                    ,'".odbc_result($rs, 23)."','".odbc_result($rs, 24)."','".odbc_result($rs, 25)."','".odbc_result($rs, 26)."'
                    ,'".odbc_result($rs, 27)."','".odbc_result($rs, 28)."','".odbc_result($rs, 29)."','".odbc_result($rs, 30)."' 
                    ,'".odbc_result($rs, 31)."') ";
        }
}
    
    $sql .= implode(',', $arr); 
    $db->sql_query($sql); 
}
exit('suc');
odbc_close($cn);

 
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: The connection has timed out

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

$i=110 คืออะไรครับ
ตามโค้ด odbc_exec() ซ้ำซ้อน Loop while ทำงานซ้ำ
ลองปรับส่วนที่ทำงานซ้ำให้ทำงานครั้งเดียว หรือ ทำงานเป็นช่วงๆ
ลองปรับ odbc_result เป็น odbc_fetch_row() ,odbc_fetch_array() แทน ลดการเรียก function ซ้ำซ้อน
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
buay
PHP Super Member
PHP Super Member
โพสต์: 250
ลงทะเบียนเมื่อ: 02/06/2014 9:55 am

Re: The connection has timed out

โพสต์ที่ยังไม่ได้อ่าน โดย buay »

เปลี่ยนไป select ข้อมูลครั้งเดียว เก็บไว้ที่ตัวแปลเป็น array ก่อน แล้วค่อยมาเข้า loop insert ข้อมูล

แต่เจอปัญหา memory เต็มแทน เนื่องจาก odbc มันใช้ limit ไม่ได้ ต้องดึงมาทั้งหมด ทำให้ข้อมูลเยอะ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: The connection has timed out

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ทางแก้
1. config เพิ่ม memmory
2. ปรับ query เราเพิ่ม limit ไม่ได้ แต่สามารถ ใช้เงื่อนไข WHERE ชื่อ field ที่ ไว้อ้างได้เช่น field id หรือ field เวลาที่ บันทึก record
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
buay
PHP Super Member
PHP Super Member
โพสต์: 250
ลงทะเบียนเมื่อ: 02/06/2014 9:55 am

Re: The connection has timed out

โพสต์ที่ยังไม่ได้อ่าน โดย buay »

ทำได้แล้วค่ะ โดยการเพิ่ม memmory

ขั้นตอนแรก select ข้อมูลมาก่อน ใส่ไว้ในตัวแปร array แล้วนำ array มาทำการวน loop อีกที

โค้ด: เลือกทั้งหมด

$cn = odbc_connect("pro_mdb2", "", "") or die("ติดต่อฐานข้อมูลไม่ได้");
$rs = odbc_exec($cn, 'SELECT * FROM WDATA ') or die("รัน SQL ไม่ผ่าน");
$data_arr = array();
while( $row = odbc_fetch_array($rs) ) {
    $data_arr[] = $row;
} 
$limit = 500;
$count = count($data_arr);
$loop = ceil($count/$limit);
for($i=0;$i<$loop;$i++){   
        $sql = 'INSERT INTO WDATA (field1,field2,...) VALUES ';
        
        $arr = array();    
        for($j = $limit * $i, $c = $j + $limit; $j < $c; $j++) {
            $v = $data_arr[$j];
            if($j == $count){
                break;
            }
            $truck = iconv( 'TIS-620', 'UTF-8', $v[field2]);            
            $arr[] = " ('".$v[field1]."','".$truck."','".$v[field3]."',......) ";
  
        }
        $sql .= implode(',', $arr);  
        $db->sql_query($sql); 
}
odbc_close($cn);
 
จากนั้นทำการตรวจสอบเงื่อนไขให้ทำการวน loop ข้อมูลรอบละ 500
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 58