Bài đăng

Hiển thị các bài đăng có nhãn PHP

PHP + sqlite cách sử dụng cơ bản

Kết nối đến file CSDL $db = new SQLite3($options['path']); Thực thi một câu sql: CREATE, UPDATE, DELETE,… $sql = "CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, username TEXT NOT NULL, password TEXT NOT NULL, status NUMERIC );" $db->exec($sql); Select data $sql = "SELECT * FROM users limit 1"; $results = $db->query($sql); $rows = array(); while ($row = $results->fetchArray(SQLITE3_ASSOC)) { $rows[] = $row; } print_r($rows);

Làm việc với JSON trong PHP

Làm việc với JSON trong PHP. Chuyển array thành json string $user = [ 'id' => 1 , 'username' => 'admin' , ]; echo json_encode($user); Kết quả: { "id" : 1 , "username" : "admin" } Nếu muốn format đẹp hơn ta cho thêm tham số: echo json_encode( $user , JSON_PRETTY_PRINT); Kết quả: { "id" : 1 , "username" : "admin" } Lưu ý : chỉ sử làm đẹp json khi test, không nên sử dụng trong môi trường chạy thực tế vì làm tăng kích thước json string với các kí tự end-line, space,... Chuyển Object thành json string Ta giả sử có class user như sau: class User { public $id ; public $username ; } $user = new User() $user ->id = 1; $user ->username = 'admin' ; Thì tương tự như trên ta cũng có thể gọi hàm  json_encode  để encode object thành json tring. echo json_encode( $user ); Kết quả: { "id" : 1 , "username" : "admin" } Chuyển json string t...

PHP bật hiển thị lỗi

Đặt đoạn code này trước đoạn, nơi mà bạn muốn hiển thị lỗi: ini_set ( 'display_errors' , 1 ); ini_set ( 'display_startup_errors' , 1 ); error_reporting ( E_ALL );

Session memcached cho Zend 1

Session memcached cho Zend 1 File: application/Bootstrap.php <?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap  { ... protected function _initSessionId()     {     $this->bootstrap('session');         Zend_Session::getSaveHandler()->setCache(My_Memcache::getSessionCache());     } ... } File: /library/My/Memcache.php <?php class My_Memcache{     private static $_ssinstance;   public static function getSessionCache() {         if(!is_object(self::$_ssinstance))         {             $cacheConf = new Zend_Config_Ini(CONFIG_PATH . '/cache.ini', APPLICATION_ENV);              $memcache = $cacheConf->memcached;             self::$_ssinstance = Zend_Cache::factory($memcache->frontend->name,          ...

php regex match is absolute url

Tìm các url có "/" đầu tiên if (preg_match('/^\//', $url, $out)) $url = STATIC_URL.$url;

Call to undefined function curl_init() Install cURl cho Linux

Bạn chưa cài gói curl chỉ cần cài vào là giải quyết được. $sudo apt-get install curl libcurl3 libcurl3-dev php5-curl