首页 >后端开发 >php教程 >PHP PDO基础

PHP PDO基础

WBOY
WBOY原创
2016-07-29 09:13:051227浏览

<code><span>$dbc</span><span>=</span><span>"mysql"</span>;          <span>//数据库类型</span><span>$dbname</span><span>=</span><span>"account"</span>;     <span>//数据库名称</span><span>$user</span><span>=</span><span>"root"</span>;          <span>//帐号</span><span>$password</span><span>=</span><span>"root"</span>;  <span>//密码</span><span>$host</span><span>=</span><span>"localhost"</span>;     <span>//主机名称</span><span>$dsn</span><span>=</span><span>"$dbc:host=$host;dbname=$dbname"</span>; <span>//DSN</span><span>$pdo</span><span>=</span><span>new</span> PDO(<span>$dsn</span>,<span>$user</span>,<span>$password</span>);        <span>//实例化对象</span><span>$query</span><span>=</span><span>"insert into user(username,password,class) values ('test','test','1')"</span>;
<span>$result</span><span>=</span><span>$pdo</span><span>-></span>exec(<span>$query</span>);                <span>//PDO::exec 执行insert delete update 操作 返回值为受影响的行数</span>
echo <span>$result</span>;


<span>$result</span><span>=</span><span>$pdo</span><span>-></span>query(<span>"select * from user"</span>);  <span>//PDO::query 执行查询</span>
foreach(<span>$result</span> as <span>$row</span>){                  <span>//通过foreach 输出</span>
    print_r(<span>$row</span>);
    echo <span>""</span>;
}

<span>$result</span><span>=</span><span>$pdo</span><span>-></span>prepare(<span>"select * from usfdser"</span>); <span>//预处理语句</span><span>//多次查询 建议用这种方法</span><span>$result</span><span>-></span>execute();                                <span>//prepare()准备查询 execute()执行</span><span>while</span>(<span>$rs</span><span>=</span><span>$result</span><span>-></span>fetch(PDO<span>::FETCH_BOTH</span>)){ <span>//获取结果集的下一行</span><span>/*
                                                PDO::FETCH_ASSOC    关联数组
                                                PDO::FETCH_NUM      数字<strong>索引</strong>数组
                                                PDO::FETCH_BOTH     两种都有 默认
                                                PDO::FETCH_OBJ      对象形式
                                                PDO::FETCH_BOUND    布尔值形式
                                                PDO::FETCH_LAZY     关联 数字<strong>索引</strong> 和对象 三种
                                                */</span>
    print_r(<span>$rs</span>);
    echo <span>""</span>;
}
echo <span>$result</span><span>-></span>rowCount();              <span>//返回行数</span>
echo <span>$result</span><span>-></span>columnCount();           <span>//返回列数</span><span>$rs</span><span>=</span><span>$result</span><span>-></span>fetchAll();                <span>//获取结果集中的所有行</span>
print_r(<span>$rs</span>);

<span>$er</span><span>=</span><span>$result</span><span>-></span>errorCode();              <span>//获取错误 由五个数字和字母组成的代码</span>
echo <span>$er.</span><span>""</span>;
print_r(<span>$result</span><span>-></span>errorInfo());         <span>//显示错误信息</span>
try{
    <span>$pdo</span><span>=</span><span>new</span> PDO(<span>$dsn</span>,<span>$user</span>,<span>$password</span>);      <span>//实例化对象</span><span>$pdo</span><span>-></span>setAttribute(PDO<span>::ATTR_ERRMODE</span>,PDO<span>::ERRMODE_WARNING</span>);    <span>//设置成警告模式</span><span>$result</span><span>=</span><span>$pdo</span><span>-></span>prepare(<span>"select * from usfdser"</span>);    
    <span>$result</span><span>-></span>execute();
}catch(PDO<strong>Exception</strong> <span>$e</span>){
    die(<span>"ERROR!:"</span><span>.</span><span>$e</span><span>-></span>getMessage());
}

try{
    <span>$pdo</span><span>=</span><span>new</span> PDO(<span>$dsn</span>,<span>$user</span>,<span>$password</span>);      <span>//实例化对象</span><span>$pdo</span><span>-></span>setAttribute(PDO<span>::ATTR_ERRMODE</span>,PDO<span>::ERRMODE_<strong>Exception</strong></span>);  <span>//设置成异常模式</span><span>$result</span><span>=</span><span>$pdo</span><span>-></span>prepare(<span>"select * from fdsf"</span>);
    <span>$result</span><span>-></span>execute();
}catch(PDO<strong>Exception</strong> <span>$e</span>){
    echo <span>"Error:"</span><span>.</span><span>$e</span><span>-></span>getMessage()<span>.</span><span>""</span>;
    echo <span>"Code:"</span><span>.</span><span>$e</span><span>-></span>getCode()<span>.</span><span>""</span>;
    echo <span>"File:"</span><span>.</span><span>$e</span><span>-></span>getFile()<span>.</span><span>""</span>;
    echo <span>"Line:"</span><span>.</span><span>$e</span><span>-></span>getLine()<span>.</span><span>""</span>;
    echo <span>"Trace:"</span><span>.</span><span>$e</span><span>-></span>getTraceAsString()<span>.</span><span>""</span>;
}</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP PDO基础,包括了Exception,索引方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:phalcon查询技巧下一篇:PHP的缓冲区