Home  >  Article  >  Backend Development  >  MySQL PDO connection and operation

MySQL PDO connection and operation

WBOY
WBOYOriginal
2016-08-08 09:21:471143browse
<p>//实例化PDO类 连接数据库  参数是 mysql:host=localhost;dbname=lamp 用户名  密码
$pdo = new PDO('mysql:host=localhost;dbname=lamp', 'root', '');</p><p>//查询语句
</p><p>$sql = "select * from lamp_login";
$stmt = $pdo->query($sql);</p><p>//增 删  改</p><p>$pdo->exec($sql);
</p><p>PDO::FETCH_OBJ相当于mysql_fetch_object</p><pre dir="ltr">object(<em>stdClass</em>)[<em>3</em>]
  <em>public</em> 'id' <span>=></span><small>string</small><span>'1'</span><em>(length=1)
  public</em> 'username' <span>=></span><small>string</small><span>'user1'</span><em>(length=5)
  public</em> 'password' <span>=></span><small>string</small><span>'123'</span><em>(length=3)</em>

PDO::FETCH_BOTH is equivalent to mysql_fetch_array

array<em>(size=6)</em>
  'id' <span>=></span><small>string</small><span>'2'</span><em>(length=1)</em>
  0 <span>=></span><small>string</small><span>'2'</span><em>(length=1)</em>
  'username' <span>=></span><small>string</small><span>'user2'</span><em>(length=5)</em>
  1 <span>=></span><small>string</small><span>'user2'</span><em>(length=5)</em>
  'password' <span>=></span><small>string</small><span>'234'</span><em>(length=3)</em>
  2 <span>=></span><small>string</small><span>'234'</span><em>(length=3)</em>

PDO::FETCH_NUM is equivalent to mysql_fetch_row

array<em>(size=3)</em>
  0 <span>=></span><small>string</small><span>'3'</span><em>(length=1)</em>
  1 <span>=></span><small>string</small><span>'user3'</span><em>(length=5)</em>
  2 <span>=></span><small>string</small><span>'345'</span><em>(length=3)</em>

PDO::FETCH_ASSOC is equivalent to mysql_fetch_assoc

array<em>(size=3)</em>
  'id' <span>=></span><small>string</small><span>'4'</span><em>(length=1)</em>
  'username' <span>=></span><small>string</small><span>'zhouyao'</span><em>(length=7)</em>
  'password' <span>=></span><small>string</small><span>'123456'</span><em>(length=6)</em>

Copyright Statement: This article is an original article by the blogger and has not been published by the blogger. No reproduction allowed with permission of the owner.

The above introduces the MySQL PDO connection and operation, including the relevant content. 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