作者:星宇ooo | 来源:互联网 | 2023-09-24 09:19
1. exec()
1.1 建表
phptry {$dsn &#61; &#39;mysql:host&#61;localhost;dbname&#61;test&#39;;$username &#61; &#39;root&#39;;$password &#61; &#39;123456&#39;;$pdo &#61; new PDO($dsn,$username,$password);$sql &#61; <<<EOF
create table if not exists user(id int unsigned auto_increment key,username varchar(20) not null unique,password char(32) not null,email varchar(32) not null
)
EOF;$res &#61; $pdo->exec($sql);var_dump($res);} catch (PDOException $e) {echo $e->getMessage();
}返回 0&#xff1b;
插入记录&#xff1a;
//插入一条记录//$sql &#61; &#39;insert user(username,password,email) values("wjh",md5("wjh"),"188&#64;qq.com")&#39;;//$res &#61; $pdo->exec($sql); 返回 1//插入多条记录
$sql &#61; <<values("wjh1",md5("wjh"),"188&#64;qq.com"),("wjh2",md5("wjh"),"188&#64;qq.com"),("wjh3",md5("wjh"),"188&#64;qq.com")
EOF;$res &#61; $pdo->exec($sql); // 返回 3$pdo->lastInsertId(); //返回 最后一条插入记录的 ID
更新记录&#xff1a;$sql &#61; &#39;update user set username&#61;"immoc" where id&#61;3&#39;;$res &#61; $pdo->exec($sql);
删除记录$sql &#61; &#39;delete from user where id&#61;2&#39;;$res &#61; $pdo->exec($sql);
获取错误信息&#xff1a;$sql &#61; &#39;delete from user12 where id&#61;2&#39;;$res &#61; $pdo->exec($sql);if ($res &#61;&#61;&#61; false) {echo $pdo->errorCode();echo &#39;
&#39;;$errInfo &#61; $pdo->errorInfo();var_dump($errInfo);} else {var_dump($res);}
2. query()
try {$pdo &#61; new PDO(&#39;mysql:host&#61;localhost;dbname&#61;test&#39;,&#39;root&#39;,&#39;123456&#39;);$sql &#61; &#39;select * from user;&#39;;$stmt &#61; $pdo->query($sql);foreach ($stmt as $row) {echo $row[&#39;id&#39;];echo &#39;
&#39;;echo $row[&#39;username&#39;];echo &#39;
&#39;;echo $row[&#39;password&#39;];echo &#39;
&#39;;echo $row[&#39;email&#39;];echo &#39;
&#39;;}} catch (PDOException $e) {echo $e->getMessage();
}
3.prepare() and execute()
try {$pdo &#61; new PDO(&#39;mysql:host&#61;localhost;dbname&#61;test&#39;,&#39;root&#39;,&#39;123456&#39;);$sql &#61; "select * from user";$stmt &#61; $pdo->prepare($sql);$stmt->setFetchMode(PDO::FETCH_OBJ);$res &#61; $stmt->execute();
if ($res) {while($row &#61; $stmt->fetch(PDO::FETCH_ASSOC)) {var_dump($row );}}} catch (PDOException $e) {echo $e->getMessage();
}
4.getAttribute() and setAttribute()
try {$dsn &#61; &#39;mysql:host&#61;localhost;dbname&#61;test&#39;;$username &#61; &#39;root&#39;;$password &#61; &#39;123456&#39;;$pdo &#61; new PDO($dsn,$username,$password);echo &#39;自动提交 : &#39; . $pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);echo &#39;
&#39;;echo &#39;默认错误处理模式 &#xff1a;&#39; . $pdo->getAttribute(PDO::ATTR_ERRMODE);echo &#39;
&#39;;$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);echo &#39;自动提交 : &#39; . $pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);
} catch (PDOException $e) {echo $e->getMessage();
}
try {$dsn &#61; &#39;mysql:host&#61;localhost;dbname&#61;test&#39;;$username &#61; &#39;root&#39;;$password &#61; &#39;123456&#39;;$pdo &#61; new PDO($dsn,$username,$password);$attArr &#61; array(&#39;AUTOCOMMIT&#39;,&#39;CASE&#39;,&#39;CLIENT_VERSION&#39;,&#39;CONNECTION_STATUS&#39;,&#39;DRIVER_NAME&#39;,&#39;ERRMODE&#39;,&#39;ORACLE_NULLS&#39;,&#39;PERSISTENT&#39;,&#39;PREFETCH&#39;,&#39;SERVER_INFO&#39;,&#39;SERVER_VERSION&#39;,&#39;TIMEOUT&#39;);foreach ($attArr as $attr) {echo "PDO_ATTR_$attr : ";echo $pdo->getAttribute(constant("PDO::ATTR_$attr"));echo &#39;
&#39;;}} catch (PDOException $e) {echo $e->getMessage();
}