I'm late again! -PDO, lazy! -PDO. I'm late again! -PDO, lazy! -It's been several days since PDOhi, and it's been a week since my foot hurts. It took so long for me to realize that, for young people, or more specifically, I was lazy and came back when I got hurt! -PDO, lazy! -PDO
Hi
It's been several days, and my feet have been hurt for a week. It took so long for me to realize that for young people, or more specifically, the most uncomfortable thing about injury is not the instant body pain, not the psychological burden that comes with it, it is not the feeling of being alone and helpless. what hurts the most is fighting spirit. it is the will that cannot be raised after the injury. it is the heart that breaks through the jar, or, it is an inexplicable fear of the nature of the problem.
Fortunately, even though I haven't been active for so many days, the treasure double-edged sword still hurts my sense of responsibility and guilt. I can continue to write my blog and live ~~
Strive for a class every day. Before you go home, you can get it done. come on!
New Weather course for newcomers ~
1. PDO
1. Introduction to PDO
1.1 PDO
PHP Data Object,The database access abstraction layer unifies various database access interfaces.
ImprovePortability and Maintainability-- The method is to improve abstraction and unify access interfaces.
No matter what database is used, you can use the same API for writing.
-- Features
Encoding consistency;
Flexibility;
High performance (written in C and compiled into PHP)
OOP.
-- Supported libraries
PDO is just an abstract interface. operations on this interface also need to support various libraries.
Here we use the PHP language, so we use PDO MySQL.
1.2 configure and enable
Configure in the PHP configuration file:
Start php_pdo.dll expansion-> open php_pdo_mysql.dll-> phpinfo.
Of course, different environments (integration) are different. baidu.
1.3 connect to the database
-- Method
Parameter format;
URI format;
Configuration file format. Php. ini
-- Chestnut
/*
* PDO implements database connection
*/
// Parameter format
Try {
$ Dsn = 'MySQL: localhost; dbname = imooc_pdo ';// Data source
$ Username = 'root ';
$ Passwd = '';
$ Pdo = new PDO ($ dsn, $ username, $ passwd); // pdo object
Var_dump ($ pdo );
} Catch (PDOException $ e) {// Get error message
Echo $ e-> getMessage ();
}
// URI format
Try {
$ Dsn = 'uri: C: \ wamp \ www \ PDO_learning \ dsn.txt ';// The difference is that the data source is obtained.
$ Username = 'root ';
$ Passwd = '';
$ Pdo = new PDO ($ dsn, $ username, $ passwd );
Var_dump ($ pdo );
} Catch (PDOException $ e) {// Get error message
Echo $ e-> getMessage ();
}
// In the configuration file format, you must first write pdo. dsn. imooc = "mysql: host = localhost; dbname = imooc_pdo" in php. ini"
Try {
$ Dsn = 'imooc ';//The difference is that the data source is obtained.
$ Username = 'root ';
$ Passwd = '';
$ Pdo = new PDO ($ dsn, $ username, $ passwd );
Var_dump ($ pdo );
} Catch (PDOException $ e) {// Get error message
Echo $ e-> getMessage ();
}
The basic process is to write the parameters and then create a new PDO object.. Parameters include data source information, user name, and password.
Data source: data source name: Drive name: host; database; (the syntax of various databases is different, self-check ).
We can see that the difference between the three forms is that the method for obtaining data sources is different.
We recommend that you use the parameter format to connect.
II. Use of PDO objects (add, delete, modify, and query)
2.1 exec ()
Execute an SQL statement and return the affected number of rows. If no affected record is returned, the return value is 0.
Note that exec is useless to select.
-- Chestnut: table creation & adding
/*
* Exec () use cases
*/
$ Pdo = new PDO ('MySQL: host = localhost; dbname = imooc ', 'root ','');
$ SQL = <
Create table if not exists user (
Id int unsigned primary key AUTO_INCREMENT,
Username VARCHAR (20) not null unique,
Password CHAR (32) not null,
Email VARCHAR (30) NOT NULL
);
EOF;
$ Pdo-> exec ('use imooc_pdo ');
$ Result0 = $ pdo-> exec ($ SQL );
Var_dump ($ result0 );
$ Insert = 'Insert user (username, password, email) values ("Kinga", "'. md5 ('king').'", "shit@shit.com ")';
$ Result1 = $ pdo-> exec ($ insert );
Var_dump ($ result1 );
However, if the execution is repeated, false is returned. (? I don't know why, but who can tell me)
The same deletion is also possible, but check whetherSelect cannot be executed using exec..
Each time the result is determined from the exec return result.
---------------------------------------------------------------------
Oh, I really don't want to fight. I watched two variety shows from the court .. Come on tomorrow.
Success! -PDO, lazy! -It's been several days since PDO hi, and my foot hurts for a week. It took so long for me to discover that young people, or more specifically, were the most injured...