Tag Archives: php

Đẩy dữ liệu từ mảng vào database

Mảng thì to mà thân em MySQL thì nhỏ!
MySQL cho phép chèn nhiều records qua một câu lệnh kiểu như sau
# Câu lệnh insert multiple:
INSERT INTO bảng VALUES (bộ giá trị thứ nhất), (bộ giá trị thứ hai), … (bộ giá trị thứ N);

Vì thế khi đẩy dữ liệu từ mảng vào database ta có thể tạo ra câu lệnh SQL như vậy, thay vì với mỗi phần tử của mảng ta lại tạo ra một bộ và INSERT INTO bảng VALUES (bộ giá trị có được);

Tuy nhiên do cấu hình max_allowed_packet, ta chỉ thực thi được câu lệnh chèn đồng thời 1 phát nhiều bản ghi với chỉ số N giới hạn.

Do đó, code phân tách mảng và generate ra các câu SQL nên làm như sau
<?php
$prefixSQL = 'INSERT INTO phimconheo(id, title, description, source, price, available) VALUES ';
$i = 0;
foreach ($data as $record) {
    $id = intval($record['id']);
    $title = addslashes($record['title']);
    // same above for other fields
    
    $sql .= "($id, $title, $description, $source, $price, $available),";
    $i++;
    if ($i == LIMIT_EXECUTABLE) {
        $sql = substr($sql, 0, strlen($sql) – 1) . ";";
        $sql = $prefixSQL . $sql;
        echo $sql . "\n";
        $i = 0;
        $sql = '';
    }
}

if ($sql != '') {
    $sql = substr($sql, 0, strlen($sql) – 1) . ";";
    $sql = $prefixSQL . $sql;
    echo $sql . "\n";
}

Handle timeout for SOAP Client

<?php
class SoapClientTimeout extends SoapClient
{
    private $timeout;

    public function __setTimeout($timeout)
    {
        if (!is_int($timeout) && !is_null($timeout))
        {
            throw new Exception("Invalid timeout value");
        }

        $this->timeout = $timeout;
    }

    public function __doRequest($request, $location, $action, $version, $one_way = FALSE)
    {
        if (!$this->timeout)
        {
            // Call via parent because we require no timeout
            $response = parent::__doRequest($request, $location, $action, $version, $one_way);
        }
        else
        {
            // Call via Curl and use the timeout
            $curl = curl_init($location);

            curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
            curl_setopt($curl, CURLOPT_HEADER, FALSE);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
            curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);

            $response = curl_exec($curl);

            if (curl_errno($curl))
            {
                throw new Exception(curl_error($curl));
            }

            curl_close($curl);
        }

        // Return?
        if (!$one_way)
        {
            return ($response);
        }
    }
}

Test Memcache

<?php
$startTime = microtime(true);
$post_id = isset($_GET['p']) ? $_GET['p'] : 'xyz';
$memcached = new Memcache;
$memcached->connect('localhost', 11211);
if (($rows = $memcached->get('post_id_' . $post_id)) === false) {
    echo 'Init cache<br />';
    //yes this is safe, we type casted it already ;)
    mysql_connect('localhost:3306', 'root', 'Clip2008');
    $rs = mysql_query('SHOW TABLES IN mysql');
    if ($rs && mysql_num_rows($rs) > 0) {
        $rows = array();
        while ($row = mysql_fetch_assoc($rs)) {
            $rows[] = $row;
        }
        // cache compressed for 1 hour
        $memcached->set('post_id_' . $post_id, $rows, MEMCACHE_COMPRESSED, time() + 3600);
    }
} else {
    echo 'Load from cache<br />';
}
var_dump($rows);

echo microtime(true) – $startTime;

Dải IP của các nhà cung cấp mạng (ISP) ở Việt Nam

“VNPT” => array(
“203.162.0.0/16”,
“203.210.128.0/17”,
“221.132.0.0/18”,
“203.160.0.0/23”,
“222.252.0.0/14”,
“123.16.0.0/12”),

“FPT” => array(
“210.245.0.0/17”,
“58.186.0.0/15”,
“58.187.0.0/15”,
“118.68.0.0/14”),

“Viettel” => array(
“203.113.128.0/18”,
“220.231.64.0/18”,
“125.234.0.0/15”,
“117.0.0.0/13”),

“SPT” => array(
“221.133.0.0/19”,
“221.121.0.0/18”,
“116.118.0.0/17”),

“HTC” => array(
“203.128.240.0/21”,
“202.60.104.0/21”),

“EVN” => array(
“203.190.160.0/20”,
“125.214.0.0/18”,
“125.212.128.0/17”,
“116.96.0.0/12”)