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>"</br>"</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>"</br>"</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>"</br>"</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>"</br>"</span>;
    echo <span>"Code:"</span><span>.</span><span>$e</span><span>-></span>getCode()<span>.</span><span>"</br>"</span>;
    echo <span>"File:"</span><span>.</span><span>$e</span><span>-></span>getFile()<span>.</span><span>"</br>"</span>;
    echo <span>"Line:"</span><span>.</span><span>$e</span><span>-></span>getLine()<span>.</span><span>"</br>"</span>;
    echo <span>"Trace:"</span><span>.</span><span>$e</span><span>-></span>getTraceAsString()<span>.</span><span>"</br>"</span>;
}</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

위 내용은 Exception, Indexing 등 PHP PDO의 기본 사항을 소개한 내용으로, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:phalcon 쿼리 스킬다음 기사:phalcon 쿼리 스킬