Home  >  Article  >  Backend Development  >  PHP PDO

PHP PDO

WBOY
WBOYOriginal
2016-07-29 08:56:54982browse

PHP Data Objects (PDO) extension defines a lightweight, consistent interface for PHP to access databases.

<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>

PDO operation transaction

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

Returns the ID of the last inserted row
PDO::lastInsertID()

<

exec() execution
Compared with query(), exec() returns the number of affected rows

$sql = "insert into table values('$val')";
if(false===$pdo->exec($sql)){
    echo'执行失败';
}

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above is an introduction to PDO in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn