My lazy eggs are back! -pdo, lazy egg!-pdo_php Tutorial

Source: Internet
Author: User
Tags dsn php language

My lazy eggs are back! -pdo, lazy egg!-pdo


Hi

For a few days, the foot injury has been a week. Play so long to find that, for the young, or more specifically, for me, the most uncomfortable injury is not the injury of the moment of the body pain, not with the psychological burden, not alone a person far away from the lonely feeling of helplessness, the most hurt is the morale, is injured after the depression will not rise, is broken broken falls erosion heart, or, Is the inner inexplicable fear of the nature of the problem.

Fortunately, although so many days did not move, the fear of the double-edged sword is still a stinging sense of responsibility, guilt, I can continue to write blog, Alive ~ ~

Fight for a day a class, go home before you can have, together refueling!

New Course for newcomers

1. PDO

I. INTRODUCTION of PDO

1.1 PDO

PHP data Object, the database Access abstraction layer, unified the various database access interface.

Improve the portability and maintainability between databases-the means is the increase of abstraction, the unification of the Access interface.

No matter what database you use, you can write it with the same API.

--Features

Coding consistency;

Flexibility

High performance (written in C, compiled as PHP)

Oop.

--Supported libraries

PDO is just an abstract interface, and the operation of the interface needs to support a variety of libraries.

Here we use the PHP language, so with PDO MySQL.

1.2 Configuring and enabling

Configure the configuration file in PHP:

Open Php_pdo.dll Extension-"open the corresponding database expansion php_pdo_mysql.dll-" phpinfo look.

Of course different environment (integration) is not the same, self-Baidu bar.

1.3 Connecting the Database

--Way

Parameter form;

URI form;

Configuration file Form. Ini

--Chestnut

/*
* PDO Implementation Connection database
*/

Parameter form
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 form
try {
$dsn = ' uri:c:\wamp\www\pdo_learning\dsn.txt '; The difference is that the data source gets
$username = ' root ';
$passwd = ";
$pdo =new PDO ($DSN, $username, $passwd);
Var_dump ($PDO);
}catch (pdoexception $e) {//Get error message
echo $e->getmessage ();
}

Configuration file form, first to write pdo.dsn.imooc= "Mysql:host=localhost;dbname=imooc_pdo" in php.ini
try {
$dsn = ' IMOOC ';//The difference is in the data source acquisition
$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 new to a PDO object . Parameters have data source information, user name, password three.

DataSource: Data Source Name: drive name: host; database; (various databases here the syntax is different, self-examination).

As you can see, the three forms differ in the way data sources are taken.

It is recommended to connect by Parameter form, of course, as you please.

Second, the use of PDO objects (increase and deletion of the search)

2.1 Exec ()

Executes an SQL statement and returns the number of rows affected if no affected records are returned as 0.

Note that EXEC has no use for select.

--Chestnut: Build table & Increase

/*
* Use case for EXEC ()
*/

$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 () not NULL UNIQUE,
Password CHAR (+) not NULL,
Email VARCHAR (+) not NULL
);
EOF;
$pdo->exec (' use imooc_pdo ');
$result 0= $pdo->exec ($sql);
Var_dump ($result 0);

$insert = ' Insert User (Username,password,email) VALUES ("Kinga", "'. MD5 (' King '). '", "shit@shit.com") ';
$result 1= $pdo->exec ($insert);
Var_dump ($result 1);

However, if executed repeatedly, false is returned. (? I do not know why, who can tell me)

It is also possible to delete the same, but note that select cannot be executed with exec .

Each time a result is judged from the return result of the exec.

---------------------------------------------------------------------

Oops, I really owe a dozen, saw two sets of court's variety show, the time is rushing. Let's cheer up tomorrow.

http://www.bkjia.com/PHPjc/1081946.html www.bkjia.com true http://www.bkjia.com/PHPjc/1081946.html techarticle My lazy eggs are back! -pdo, lazy egg!-pdo hi for a few days, foot injury has been a week. It took so long to realize that for young people, or more specifically, for me, the injury was the most ...

  • 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.