PHP PDO基础

WBOY
Lepaskan: 2016-07-29 09:13:05
asal
1227 orang telah melayarinya

<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>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan