php mysql 資料操作問題

來源:互聯網
上載者:User
求前輩指導!

,表裡面有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" );

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.