For more information about phpmysql data operations, see our predecessors!
The table contains two pieces of data. The first one is inserted in phpMyAdmin, and the second one is inserted from the php page.
Org_name varchar type open_time timestamp type
The php code is as follows:
Include_once ("pay/CommonUtil. php ");
Include_once dirname (_ FILE _). '/db/DbOperation. php ';
$ Db = new DbOperation ('Z _ org ');
$ Org = array (
"Org_id" => 2,
"Org_name" => "Beijing ",
"Open_time" => time ()
);
$ Db-> addObject ($ org );
$ Rs = $ db-> getAll ();
Var_dump ($ rs );
?>
Problem:
1. garbled Chinese characters inserted from php. The time is 0000-00-00 00:00:00
2. query the first data in the entire table with garbled characters. the second data entry is normal.
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) "Beijing" ["open_time"] => string (19) "0000-00-00 00:00:00 "}}
Reply to discussion (solution)
What is program code? The same as phpmyadmin encoding.
The program code is UTF-8 and phpMyAdmin is UTF-8.
Du Niang said to update (set names utf_8) when obtaining database connections)
After I add it, the operation reports the error Fatal error: Uncaught exception 'DB _ exception' with message'. Update failed: Unknown character set: 'utf''
Yes
('Set names utf8 ')
No
('Set names utf_8 ')
Well, I tried it with utf8. this problem is fixed.
The remaining timestamp problem is: the time () function does not work. I tried date ("Y-m-d h: I: s ")
The problem is that the write time is:
Array (3) {["org_id"] => string (1) "1" ["org_name"] => string (9) "Shanghai" ["open_time"] => string (19) "18:35:18 "}
Array (3) {["org_id"] => string (1) "2" ["org_name"] => string (20) "isn ?? ? "[" Open_time "] => string (19)" 0000-00:00:00-00 "}
Array (3) {["org_id"] => string (1) "3" ["org_name"] => string (9) "Chongqing" ["open_time"] => string (19) "0000-00-00 00:00:00 "}
Array (3) {["org_id"] => string (1) "4" ["org_name"] => string (9) "Tianjin" ["open_time"] => string (19) "12:00:31 "}
Array (3) {["org_id"] => string (1) "5" ["org_name"] => string (9) "Hebei province" ["open_time"] => string (19) "12:02:34 "}
It is eight hours slower than the normal time. I don't know if it is a problem with the time zone. how should I set it?
$ Org = array (
"Org_id" => 2,
"Org_name" => "Beijing ",
"Open_time" => 'Now ()'
);
Good database time functions
The time you get date is incorrect because your tree area is not set.
I added date_default_timezone_set ('prc') to get the database connection ');
The write time is ["open_time"] => string (19) "08:11:27", which does not seem to be in 24-hour format.
I just tried your "open_time" => 'Now () 'and got ["open_time"] => string (19) "0000-00-00 00:00:00 ", I have escaped all write parameters using addslashes. I don't know if this is the problem.
Php does not have the now () function
Now () is equivalent to date ("Y-m-d H: I: s ");