1. 程式人生 > >PHP將陣列存入到資料庫中

PHP將陣列存入到資料庫中

<?php //1.implode和explode方式 //2.print_r和自定義函式方式 //3.serialize和unserialize方式 //4.json_encode和json_decode方式 // 如果想執行該檔案,需要建立資料庫admin,和資料表test,或者修改程式碼 // //--------------------------------------------------------------- // CREATE TABLE `test` ( // `id` int(10) unsigned NOT NULL AUTO_INCREMENT key,
// `array` text, // ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; //定義用print_r將陣列儲存到資料庫中的類 header('content-type:text/html; charset=utf8'); define("DB_HOST","localhost"); define("DB_USER","root"); define("DB_PWD","0227"); define("DB_DBNAME","admin"); define("DB_CHARSET","utf8"); // 定義逆置print_r值的類
class Trie { protected $dict = array(); protected $buf = ''; function set($word, $value='') { if(is_array($word)) foreach($word as $k=>$v) $this->set($k, $v); $p =& $this->dict; foreach(str_split($word) as $ch) { if(! isset($p[$ch])) $p[$ch] = array(); $p
=& $p[$ch]; } $p['val'] = $value; return $this; } function parse($str) { $this->doc = $str; $this->len = strlen($str); $i = 0; while($i < $this->len) { $t = $this->find($this->dict, $i); if($t) { $i = $t; $this->buf = ''; }else $this->buf .= $this->doc{$i++}; } } protected function find(&$p, $i) { if($i >= $this->len) return $i; $t = 0; $n = $this->doc{$i}; if( isset($p[$n]) ) $t = $this->find($p[$n], $i+1); if($t) return $t; if( isset($p['val']) ) { $ar = explode(',', $p['val']); call_user_func_array( array($this, array_shift($ar)), $ar ); return $i; } return $t; } function __call($method, $param) { echo "****\n$this->buf 未定義方法:$method 引數:" . join(',', $param) . "<br />\n"; } } class App extends Trie { public $res = array(); protected $stack = array(); protected $keyname = ''; protected $buf = ''; function __construct() { $this->stack[] =& $this->res; } protected function group() { if(! $this->keyname) return; $cnt = count($this->stack) - 1; $this->stack[$cnt][$this->keyname] = array(); $this->stack[] =& $this->stack[$cnt][$this->keyname]; $this->keyname = ''; } protected function brackets($c) { $cnt = count($this->stack) - 1; switch($c) { case ')': if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf); $this->keyname = ''; array_pop($this->stack); break; case '[': if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf); break; case ']': $this->keyname = $this->buf; } $this->buf = ''; } } //類結束 // // //連線資料庫 function connect(){ $link = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("資料庫連線失敗ERR:".mysql_errno().":".mysql_error()); mysql_select_db(DB_DBNAME) or die("開啟資料庫失敗");//mysql_errno()即顯示錯誤數量;mysql_error()即顯示錯誤資訊; $sql = 'set names '.DB_CHARSET; mysql_query($sql) or die ("設定字符集失敗"); return $link; } //插入資料庫函式 function insert($table, $array){ $keys = join(",",array_keys($array)); $vals = "'".join("','",array_values($array))."'"; $sql = "insert {$table}({$keys})values({$vals})"; mysql_query($sql); return mysql_insert_id(); } //提取剛剛插入的資料 function select($table){ $sql = "select array from {$table} order by id desc"; if($result = mysql_query($sql)){ $values = mysql_fetch_assoc($result); $value = array_pop($values); }else{ echo '提取失敗'; } return $value; }