Home  >  Article  >  Backend Development  >  php mysql 数据操作问题

php mysql 数据操作问题

WBOY
WBOYOriginal
2016-06-23 13:50:48701browse

求前辈指导!

如图,表里面有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" );

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn