Home > Backend Development > PHP Tutorial > Common methods for database operations using PDO

Common methods for database operations using PDO

WBOY
Release: 2016-07-29 09:15:00
Original
1121 people have browsed it

1. First let’s talk about the definition of PDO:

PDO extends to PHPaccess databasedefines a lightweight, consistent interface, which provides a data access abstraction layer, so that no matter what is used Databases can perform queries and obtain data through consistent functions. PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0, but cannot run on previous PHP versions.

I have highlighted the important parts in bold.

2. Database connection

PDO connection is relatively simple, with only 4 parameters: data source information (DSN), user name, password and parameter array.
Example:

<code><span><span><?php</span><span>$dbh</span> = <span>new</span> PDO(<span>'mysql:host=localhost;port=3306;dbname=test'</span>, <span>$user</span>, <span>$pass</span>,<span>array</span> (PDO::MYSQL_ATTR_INIT_COMMAND => <span>'SET NAMES \'UTF8\''</span>,PDO::ATTR_ERRMODE => PDO::ERRMODE_<strong>Exception</strong>));
<span>?></span></span></span></code>
Copy after login

If a connection error occurs, a PDOException exception will be thrown. The reference code is as follows:

<code><span>try</span>{
    <span>$params</span> = <span>array</span> (
                PDO::MYSQL_ATTR_INIT_COMMAND => <span>'SET NAMES \'UTF8\''</span> ,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_<strong>Exception</strong>,
        );

    <span>$dbh</span> = <span>new</span> PDO(<span>'mysql:host='</span>.<span>$mysql_server_name</span>.<span>';dbname='</span>.<span>$mysql_database</span>, <span>$mysql_username</span>, <span>$mysql_password</span>,<span>$params</span>);
    <span>echo</span><span>"DB Connect OK!!!"</span>.<span>'<br />'</span>;
}<span>catch</span>(PDO<strong>Exception</strong> <span>$e</span>){
    <span>echo</span><span>"DB Connect Error!!!"</span>.<span>$e</span>->getMessage();
    <span>exit</span>;
}</code>
Copy after login

3, execute query:

<code><span>$sql</span>=<span>'select pid,pname from project_list'</span>;

<span>foreach</span>(<span>$dbh</span>->query(<span>$sql</span>) <span>as</span><span>$row</span>){        <span>// 输出结果集中的数据</span><span>echo</span><span>$row</span>[<span>'pid'</span>].<span>'<br />'</span>;
    <span>echo</span><span>$row</span>[<span>'pname'</span>].<span>'<br />'</span>;
}</code>
Copy after login

4, execute update

<code><span>$sql</span> = <span>'insert into project_list(pname)values(\'</span>tianxin\<span>')'</span>;
<span>$rows</span> = <span>$dbh</span>-><span>exec</span>(<span>$sql</span>);
<span>echo</span><span>'更新了'</span>.<span>$rows</span>.<span>'行<br />'</span>;


<span>echo</span><span>"最终自动编号是:"</span>.<span>$dbh</span>->lastInsertId().<span>"<br/>"</span>;</code>
Copy after login

The above introduces the common methods of database operation in PDO, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template