PHP PDO学习(二) exec执行SQL
时间:2021-07-01 10:21:17
帮助过:2人阅读
try {
$pdo =
new PDO(‘mysql:host=localhost:3306;dbname=zhy‘, ‘root‘, ‘root‘
);
$sql = <<<
EOF
CREATE TABLE IF NOT EXISTS USERS(
ID INTEGER AUTO_INCREMENT
KEY,
USERNAME VARCHAR(20) NOT
NULL UNIQUE,
PASSWORD CHAR(30) NOT
NULL,
EMAIL VARCHAR(30
)
)
EOF;
$res =
$pdo->
exec(
$sql);
var_dump(
$res);
echo ‘<hr/>‘
;
$sql = ‘INSERT INTO USERS (USERNAME, PASSWORD, EMAIL) VALUES ("ZYN", "‘.
md5(‘ZYN‘).‘", "zyn@163.com");‘
;
$res =
$pdo->
exec(
$sql);
echo ‘受影响的记录条数 :‘.
$res.‘<br/>‘
;
echo ‘最后插入的ID号为:‘.
$pdo->
lastInsertId();
// var_dump($res);
echo ‘<hr/>‘
;
} catch (PDOException
$e) {
echo $e->
getMessage();
}
<?php
header(‘content-type:text/html;charset=utf-8‘);
try {
$pdo = new PDO(‘mysql:host=localhost:3306;dbname=zhy‘, ‘root‘, ‘root‘);
$sql = ‘UPDATE USERS SET USERNAME = "ZHY3" WHERE ID=1‘;
$res = $pdo->exec($sql);
echo $res.‘条记录被影响‘;
} catch (PDOException $e) {
echo $e->getMessage();
}
PHP PDO学习(二) exec执行SQL
标签: