PHP之PDO

WBOY
Lepaskan: 2016-07-29 08:56:54
asal
1020 orang telah melayarinya

PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口。

<code><span><span><?php </span><span>try</span> {
        <span>$dsn</span> = <span>"mysql:host=localhost; port=3306; dbname=wsq_hotel; charset=utf-8"</span>;
        <span>$user</span> = <span>'root'</span>;
        <span>$psw</span> =<span>'root'</span>;
        <span>$pdo</span> = <span>new</span> PDO(<span>$dsn</span>,<span>$user</span>,<span>$psw</span>);
        <span>$sql</span> = <span>'select goods_prices from wsq_goods_info where goods_id=2'</span>;
        <span>// $sql = "show database";</span><span>$res</span> = <span>$pdo</span>->query(<span>$sql</span>) <span>or</span> var_dump(<span>$pdo</span>->errorInfo());
        <span>// var_dump($res);</span><span>$mon</span> = <span>$res</span>->fetch(PDO::FETCH_ASSOC);
        <span>echo</span><span>$mon</span>[<span>'goods_price'</span>];
    } <span>catch</span> (PDOException <span>$e</span>) {
        <span>echo</span><span>$e</span>->getMessage();
    }
 <span>?></span></span></span></code>
Salin selepas log masuk

PDO操作事务

<code><span>//开启事务</span><span>beginTransacition()</span><span>//回滚</span><span>rollback()</span><span>//提交</span><span>commit()</span><span>//判断是否处于事务之中</span><span>inTransaction()</span></code>
Salin selepas log masuk

返回最后插入行的ID
PDO::lastInsertID()

exec()执行
与query()相比,exec()返回的是受影响行数

<code><span>$sql</span> = <span>"insert into table values('<span>$val</span>')"</span>;
<span>if</span>(<span>false</span>===<span>$pdo</span>-><span>exec</span>(<span>$sql</span>)){
    <span>echo</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,包括了方面的内容,希望对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