PHP operation database PDO

Source: Internet
Author: User
PHP operation database pdo php operation database
Load database driver

Access phpinfo. php to check whether the database driver has been loaded. the following shows that the mySql database driver has not been loaded.


Find the php. ini configuration file on drive C to enable mySql driver loading. for example, remove the semicolon.

Connect to database

// Connect to the database

$ Dsn = "mysql: dbname = test; host = 127.0.0.1"; // data source name

$ User = "root"; // user name

$ Password = "715632"; // password

Try {

$ PdoConn = new PDO ($ dsn, $ user, $ password );

Echo "database connection successful ";

} Catch (PDOException $ e ){

Echo "database connection failed". $ e-> getMessage ();

Exit;

}

Database operations

// Connect to the database

$ Dsn = "mysql: dbname = test; host = 127.0.0.1"; // data source name

$ User = "root"; // user name

$ Password = "715632"; // password

Try {

$ PdoConn = new PDO ($ dsn, $ user, $ password );

Echo "database connection successful "."
";

} Catch (PDOException $ e ){

Echo "database connection failed". $ e-> getMessage ();

Exit;

}

Try {

// Insert operation

/* $ SQL = "insert into contacts (name, telno, email) values (?,?,?) ";

$ Ptmt = $ pdoConn-> prepare ($ SQL );

$ Name = "liujun ";

$ Telno = "347535420 ";

$ Email = "liujun@qq.com ";

$ Ptmt-> bindParam (1, $ name );

$ Ptmt-> bindParam (2, $ telno );

$ Ptmt-> bindParam (3, $ email );

$ Result = $ ptmt-> execute ();

Echo "affected rows:". $ result;

*/

// Query operation

$ SQL = "select * from contacts ";

$ Ptmt = $ pdoConn-> prepare ($ SQL );

$ Result = $ ptmt-> execute ();

If ($ result = 1) {// contains the result set

While ($ list = $ ptmt-> fetch ()){

Echo $ list ['name']. "-----". $ list ['telno']. "----". $ list ['email ']."
";

}

}

} Catch (pdow.ton $ e ){

Echo "database operation failed ";

Exit;

}

Instance code: xsphp/demo. php

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.