求前輩指導!
,表裡面有2條資料 第一條是在phpMyAdmin插入的,第二條是從php頁面插入的
org_name varchar類型 open_time timestamp 類型
php代碼如下:
include_once("pay/CommonUtil.php");
include_once dirname(__FILE__).'/db/DbOperation.php';
$db = new DbOperation('z_org');
$org = array(
"org_id" => 2,
"org_name" => "北京市",
"open_time" => time()
);
$db->addObject($org);
$rs = $db->getAll();
var_dump($rs);
?>
問題:
1.從php插入的資料 漢字亂碼,時間為0000-00-00 00:00:00
2.查詢全表 第一條資料漢字亂碼,第二條正常了
array(2) { [0]=> array(3) { ["org_id"]=> string(1) "1" ["org_name"]=> string(3) "???" ["open_time"]=> string(19) "2014-08-23 18:35:18" } [1]=> array(3) { ["org_id"]=> string(1) "2" ["org_name"]=> string(9) "北京市" ["open_time"]=> string(19) "0000-00-00 00:00:00" } }
回複討論(解決方案)
程式編碼是什麼?換成和phpmyadmin編碼一樣的
程式編碼是utf-8 ,phpMyAdmin也是utf-8
度娘說 要在擷取資料庫連接時update (set names utf_8)
我加上後,運行報錯 Fatal error: Uncaught exception 'DB_Exception' with message '更新失敗:Unknown character set: 'utf''
是
('set names utf8')
不是
(‘set names utf_8')
嗯,用utf8試了,這個問題搞定了
現在剩下timestamp問題:time()函數不行,我試了date("Y-m-d h:i:s")
問題是寫入的時間為:
array(3) { ["org_id"]=> string(1) "1" ["org_name"]=> string(9) "上海市" ["open_time"]=> string(19) "2014-08-23 18:35:18" }
array(3) { ["org_id"]=> string(1) "2" ["org_name"]=> string(20) "å??京å¸?" ["open_time"]=> string(19) "0000-00-00 00:00:00" }
array(3) { ["org_id"]=> string(1) "3" ["org_name"]=> string(9) "重慶市" ["open_time"]=> string(19) "0000-00-00 00:00:00" }
array(3) { ["org_id"]=> string(1) "4" ["org_name"]=> string(9) "天津市" ["open_time"]=> string(19) "2014-08-23 12:00:31" }
array(3) { ["org_id"]=> string(1) "5" ["org_name"]=> string(9) "河北省" ["open_time"]=> string(19) "2014-08-23 12:02:34" }
比正常時間慢了8小時,不知道是不是時區的問題,應該怎麼設定?
$org = array(
"org_id" => 2,
"org_name" => "北京市",
"open_time" => 'now()'
);
用資料庫的時間函數較好
你 date 得到的時間不對,是因為你的樹區沒有設定好
我在擷取資料庫連接的地方加上了date_default_timezone_set('PRC');
寫入的時間是["open_time"]=> string(19) "2014-08-23 08:11:27" ,貌似不是24小時制了
剛試了你的"open_time" => 'now()' 得到的是["open_time"]=> string(19) "0000-00-00 00:00:00" ,我的寫入參數都用addslashes轉義過,不知道是不是這個問題
php當中沒有now()函數
now() 等價 date("Y-m-d H:i:s" );