php pdo 类 方法有哪些-PHP问题

资源魔 91 0

php pdo类办法有:一、exec办法;二、query办法;三、lastInsertId办法;四、setAttribute办法等等。

保举:《PHP视频教程》

pdo类罕用办法:

exec()

query()

lastInsertId()

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname='myweb';

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec('set names utf8');

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);

$sql="insert into user(username,age) values('user123','55')";

if($pdo->exec($sql)){
	$lastid=$pdo->lastInsertId();
	echo "ID为{$lastid}的数据拔出胜利!";
}
?>

• setAttribute()


设置猎取模式属性

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname='myweb';
 

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

$pdo->exec('set names utf8');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

$sql="select * from user";

$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo '<pre>';
print_r($rows);
echo '</pre>';
?>

***
猎取索引数组

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname='myweb';

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec('set names utf8');

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_NUM);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo '<pre>';
print_r($rows);
echo '</pre>';
?>


猎取夹杂数组

<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname='myweb';

$pdo= new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$pdo->exec('set names utf8');

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_BOTH);

$sql="select * from user";
$smt=$pdo->query($sql);
$rows=$smt->fetchAll();

echo '<pre>';
print_r($rows);
echo '</pre>';
?>

以上就是php pdo 类 办法有哪些的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 PDO php故障解决 php使用问题

抱歉,评论功能暂时关闭!