首页 >后端开发 >php教程 >PDO学习笔记

PDO学习笔记

WBOY
WBOY原创
2016-08-08 09:21:24971浏览

PDO扩展为PHP访问数据库定义了一个轻量级的、一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据。
PDO随PHP5.1发行,在PHP5.0的PECL扩展中也可以使用,无法运行于之前的PHP版本。

在PDO出现之间,在PHP中连接数据库的函数根据数据库的不同而不同。
例如,MySQL用 mysql_connect 函数, PostgreSQL数据库用 pg_connect 函数。

通过PDO编写的代码,今后数据库发生变化,只需要适当修改数据库的连接参数,不需要修改逻辑代码即可。

PDO和主要数据库的驱动同PHP一起作为扩展发布,要激活它们只需简单的编辑php.ini文件:
extension=php_pdo.dll
然后,选择针对特定数据库的DLL文件使用 dl() 在运行时加载,或者在php.ini文件中 php_pdo.dll 行后启用它们,如:

<code><span>extension=<span>php_pdo.dll</span></span><span>extension=<span>php_pdo_mysql.dll</span></span><span>extension=<span>php_pdo_pgsql.dll</span></span><span>extension=<span>php_pdo_sqlite.dll</span></span></code>

MySQL数据库连接

<code><span>$dsn</span> = <span>'mysql:dbname=yii2test;host=localhost'</span>;
<span>$user</span> = <span>'sqluser'</span>;
<span>$password</span> = <span>'sqlpassword'</span>;
<span>$db</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$user</span>, <span>$password</span>);

<span>try</span>{
    <span>$dbh</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$user</span>, <span>$password</span>);
}<span>catch</span> (PDOException <span>$e</span>){
    <span>print</span>(<span>'Error:'</span>.<span>$e</span>->getMessage());
    <span>die</span>();
}</code>

PostgreSQL数据库连接

<code><span>$dsn</span> = <span>'pgsql:dbname=yii2test host=localhost port=5432'</span>;
<span>$user</span> = <span>'sqluser'</span>;
<span>$password</span> = <span>'sqlpassword'</span>;

<span>try</span>{
    <span>$dbh</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$user</span>, <span>$password</span>);
}<span>catch</span> (PDOException <span>$e</span>){
    <span>print</span>(<span>'Error:'</span>.<span>$e</span>->getMessage());
    <span>die</span>();
}</code>

SQLite数据库连接

<code><span>$dsn</span> = <span>'sqlite:d:/sqlite/yii2test.db'</span>;
<span>$user</span> = <span>''</span>;
<span>$password</span> = <span>''</span>;

<span>try</span>{
    <span>$dbh</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$user</span>, <span>$password</span>);
}<span>catch</span> (PDOException <span>$e</span>){
    <span>print</span>(<span>'Error:'</span>.<span>$e</span>->getMessage());
    <span>die</span>();
}</code>

PDO操作MySQL数据库示范代码:

<code><span><span><?php </span><span>$dsn</span> = <span>'mysql:dbname=yii2test;host=192.168.0.69;post=3306'</span>;
<span>$user</span> = <span>'shou'</span>;
<span>$password</span> = <span>'shouadmin'</span>;

<span>try</span>{
    <span>$db</span> = <span>new</span> PDO(<span>$dsn</span>, <span>$user</span>, <span>$password</span>);

    <span>// </span><span>$sql</span> = <span>'insert into user(username, password,password_hash, status) value (?, ?, ?, ?)'</span>;
    <span>$stmt</span> = <span>$db</span>->prepare(<span>$sql</span>);
    <span>$param</span> = [];
    <span>$param</span>[] = <span>'admin'</span>. date(<span>'YmdHis'</span>);
    <span>$param</span>[] = time();
    <span>$param</span>[] =  md5(time());
    <span>$param</span>[] = <span>10</span>;
    <span>if</span>(<span>$stmt</span>->execute(<span>$param</span>)) {
        <span>echo</span><span>"insert ok !"</span> .PHP_EOL;
    } <span>else</span> {
        <span>echo</span><span>"insert ng !"</span> .PHP_EOL;
    }

    <span>// </span><span>echo</span> PHP_EOL. <span>"==> query"</span> . PHP_EOL;
    <span>$sql</span> = <span>"select * from user"</span>;
    <span>$data</span> = <span>$db</span>->query(<span>$sql</span>);
    <span>foreach</span>(<span>$data</span><span>as</span><span>$row</span>) {
      <span>echo</span><span>$row</span>[<span>"username"</span>] . <span>"  "</span> . <span>$row</span>[<span>"password"</span>]. PHP_EOL;
    }

    <span>// </span><span>echo</span> PHP_EOL. <span>"==> prepare 1"</span> . PHP_EOL;
    <span>$sql</span> = <span>'select * from user where username like ?'</span>;
    <span>$stmt</span> = <span>$db</span>->prepare(<span>$sql</span>);
    <span>$stmt</span>->execute([<span>'admin%'</span>]);
    <span>while</span>(<span>$result</span> = <span>$stmt</span>->fetch(PDO::FETCH_ASSOC)) {
        print_r(<span>$result</span>);
    }

    <span>// </span><span>echo</span> PHP_EOL. <span>"==> prepare 2"</span> . PHP_EOL;
    <span>$sql</span> = <span>'select * from user where username like :username'</span>;
    <span>$stmt</span> = <span>$db</span>->prepare(<span>$sql</span>);
    <span>$stmt</span>->execute([<span>':username'</span> => <span>'admin%'</span>]);
    <span>while</span> (<span>$result</span> = <span>$stmt</span>->fetch(PDO::FETCH_ASSOC)) {
        print_r(<span>$result</span>);
    }



}<span>catch</span> (PDOException <span>$e</span>){
    <span>print</span>(<span>'Error:'</span>.<span>$e</span>->getMessage());
    <span>die</span>();
}

<span>// 关闭数据库连接</span><span>$db</span> = <span>null</span>;
</span></span></code>

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PDO学习笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn