<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 entspricht 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 entspricht 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 entspricht 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-Erklärung: Dieser Artikel ist ein Originalartikel des Bloggers und darf nicht ohne die Erlaubnis des Bloggers reproduziert werden.
Das Obige stellt die MySQL-PDO-Verbindung und -Bedienung vor, einschließlich der relevanten Inhalte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.