The pdo database driver has a Bug that affects versions 3.1.3 and below [Severe].

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about the Bug that the pdo database driver cannot obtain the primary key of a data table. The cause of this Bug is not a logical defect, it is the carelessness of TP developers.
The ThinkPHP pdo database driver has a Bug where the primary key of the data table cannot be obtained, this Bug will cause all operations related to the primary key of the data table to be disordered, failed, or even unexpected (the primary key of the data table is id or the custom primary key in the Model is not affected ). if the primary key of your data table is not "id", an error occurs when you perform a series of operations on the database that depend on the primary key.

Versions affected by bugs: 3.1.3 and below

Bug location: Extend/Driver/Db/DbPdo.class.phpOr: Lib/Driver/Db/DbPdo.class.phpCode Location: locate the function getFields. Public function getFields ($ tableName ){
$ This-> initConnect (true );
If (C ('db _ DESCRIBE_TABLE_ SQL ')){
// Define special field query SQL
$ SQL = str_replace ('% table %', $ tableName, C ('db _ DESCRIBE_TABLE_ SQL '));
} Else {
Switch ($ this-> dbType ){
Case 'mssql ':
Case 'sqlsrv ':
$ SQL = "SELECT column_name as 'name', data_type as 'type', column_default as 'default', is_nullable as 'null'
FROM information_schema.tables AS t
JOIN information_schema.columns AS c
ON t. table_catalog = c. table_catalog
AND t. table_schema = c. table_schema
AND t. table_name = c. table_name
WHERE t. table_name = '$ tablename '";
Break;
Case 'sqlite ':
$ SQL = 'pragma table_info ('. $ tableName .')';
Break;
Case 'oracle ':
Case 'occi ':
$ SQL = "SELECT. column_name \ "Name \", data_type \ "Type \", decode (nullable, 'y', 0, 1) notnull, data_default \ "Default \", decode (. column_name, B. column_name, 1, 0) \ "pk \""
. "FROM user_tab_columns a, (SELECT column_name FROM user_constraints c, user_cons_columns col"
. "WHERE c. constraint_name = col. constraint_name AND c. constraint_type = 'p' and c. table_name = '". strtoupper ($ tableName)
. "') B where table_name ='". strtoupper ($ tableName). "'and a. column_name = B. column_name (+ )";
Break;
Case 'pgsql ':
$ SQL = 'select fields_name as "Name", fields_type as "Type", fields_not_null as "Null", fields_key_name as "Key", fields_default as "Default ", fields_default as "Extra" from table_msg ('. $ tableName. ');';
Break;
Case 'ibase ':
Break;
Case 'mysql ':
Default:
$ SQL = 'describe'. $ tableName; // Note: The driver class is not only for mysql, but not''
}
}
$ Result = $ this-> query ($ SQL );
$ Info = array ();
If ($ result ){
Foreach ($ result as $ key => $ val ){
$ Val = array_change_key_case ($ val );
$ Val ['name'] = isset ($ val ['name'])? $ Val ['name']: "";
$ Val ['type'] = isset ($ val ['type'])? $ Val ['type']: "";
$ Name = isset ($ val ['field'])? $ Val ['field']: $ val ['name'];
$ Info [$ name] = array (
'Name' => $ name,
'Type' => $ val ['type'],
'Notnull '=> (bool) (isset ($ val ['null']) & ($ val ['null'] = '')) | (isset ($ val ['notnull ']) & ($ val ['notnull'] = ''))), // not null is empty, null is yes
'Default' => isset ($ val ['default'])? $ Val ['default'] :( isset ($ val ['dflt _ value'])? $ Val ['dflt _ value']: ""),
'Primary' => isset ($ val ['dey'])? Strtolower ($ val ['dey']) = 'pri ':( isset ($ val ['pk'])? $ Val ['pk']: false ),
'Autoinc' => isset ($ val ['extra '])? Strtolower ($ val ['extra ']) = 'Auto _ secret' :( isset ($ val ['key'])? $ Val ['key']: false ),
);
}
}
Return $ info;
}
Locate this row: 'primary' => isset($val['dey'])?strtolower($val['dey']) == 'pri':(isset($val['pk'])?$val['pk']:false),You will see that the array key name originally "key" is mistakenly entered as "dey". That's right, and this Bug occurs in this line of code.

Solution: Change dey to key and the code is: 'primary' => isset($val['key'])?strtolower($val['key']) == 'pri':(isset($val['pk'])?$val['pk']:false),

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.