1.连接mysql
<?php
//数据库连接参数
$db = [
'type' => 'mysql',
'host' => '127.0.0.1',
'dbname' => 'SqlTest',
'username' => 'root',
'password' => 'root',
'port' => 3306,
'charset' => 'utf8'
];
$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']};charset={$db['charset']}";
//连接数据库
try{
$pdo = new PDO($dsn, $db['username'], $db['password']);
}
catch(PDOException $e){
die('Connection Failed: ' . $e->getMessage() );
}
?>
2.增insert
<?php
//(1) 连接数据库
require '/mysql.php';
//(2) 创建SQL语句模板,相当于占位
$sql = 'INSERT INTO `zsgc` SET `user_name`=:name, `desc`=:desc, `hobby`=:hobby, `create_time`=:ct';
//(3) 创建SQL语句对象
$stmt = $pdo->prepare( $sql );
//(4) 将变量绑定到SQL语句模板的命名占位符上
$name = 'Andrew';
$desc = '他很优秀';
$hobby = '跳伞';
$ct = time();
$stmt->bindParam('name',$name,PDO::PARAM_STR);
$stmt -> bindParam('desc',$desc,PDO::PARAM_STR);
$stmt -> bindParam('hobby',$hobby,PDO::PARAM_STR);
$stmt -> bindParam('ct',$ct,PDO::PARAM_INT);
//(5) 执行SQL操作
$add = $stmt->execute();
if( $add ){
$count = $stmt->rowCount();
if( $count>0 ){
echo '插入成功';
}
else{
echo '插入失败';
}
}
else{
die( print_r( $stmt->errorInfo(),true) );
}
//(6) 销毁pdo, 关闭连接
$pdo = null; // unset($pdo) 等价
?>
3.改update
<?php
//(1) 连接数据库
require '/mysql.php';
//(2) 创建SQL语句模板,相当于占位
$sql = 'UPDATE `zsgc` SET `email`=:email, `update_time`=:ut WHERE `id`=:id';
//(3) 创建SQL语句对象
$stmt = $pdo->prepare( $sql );
//(4) 将变量绑定到SQL语句模板的命名占位符上
$eamil = '123456@qq.com';
$ut = time();
$id = 3;
$stmt->bindParam('email', $eamil, PDO::PARAM_STR);
$stmt->bindParam('ut', $ut, PDO::PARAM_INT);
$stmt->bindParam('id', $id, PDO::PARAM_INT);
//(5) 执行SQL操作
$update = $stmt->execute();
if( $update ){
$count = $stmt->rowCount();
if( $count ){
echo '修改成功';
}
else{
echo '修改失败';
}
}
else{
die( print_r( $stmt->errorInfo(),true ) );
}
//(6) 销毁pdo, 关闭连接
$pdo = null; // unset($pdo) 等价
4.查select
<?php
//(1) 连接数据库
require '/mysql.php';
//(2) 创建SQL语句模板,相当于占位
$sql = 'SELECT * FROM `zsgc`';
//(3) 创建SQL语句对象
$stmt = $pdo->prepare( $sql );
//(4) 将变量绑定到SQL语句模板的命名占位符上
$ssq->bindParam('id',$id,PDO::PARAM_INT);
//(5) 执行SQL操作
$select = $stmt->execute();
if( $select ){
#为什么多索引下标,是pdo这个类操作出来的,fetch读取一行,fetchall读取所有行
#对于fetch( N )里的N,1去掉索引数组,加了一个queryString;2去掉索引数组;3只剩索引数组;4全部都有
// $arr = $stmt->fetch(2);
// print_r($arr);
$arrs = $stmt->fetchAll();
if( $arrs ){
foreach( $arrs as $val ){
echo '姓名:' . $val['user_name'] .'<br>';
echo '性别:' ;
if( !$val['sex'] ){
echo '女<br>';
}
else{
echo '男<br>';
}
echo '描述:' . $val['desc'] .'<br>';
echo '爱好:' . $val['hobby'] .'<br>';
echo '<hr>';
}
}
else{
echo '没有数据';
}
}
else{
die( print_r($stmt->errorInfo(),true) );
}
//(6) 销毁pdo, 关闭连接
$pdo = null; //unset($pdo);
5.删delete
<?php
// 连接数据库
require '/mysql.php';
//(2) 创建SQL语句模板,相当于占位
$sql = 'DELETE FROM `zsgc` WHERE `id`=:id';
//(3) 创建SQL语句对象
$stmt = $pdo->prepare( $sql );
//(4) 将变量绑定到SQL语句模板的命名占位符上
$id = 8;
$stmt->bindParam('id', $id, PDO::PARAM_INT);
//(5) 执行SQL操作
$delete = $stmt->execute();
if( $delete ){
if ($stmt->rowCount() > 0) {
echo '删除成功';
}
else{
echo '删除失败';
}
}
else{
die( print_r( $stmt->errorInfo(),true ) );
}
//(6) 销毁pdo, 关闭连接
$pdo = null; // unset($pdo) 等价
6.手抄作业