Blogger Information
Blog 23
fans 0
comment 1
visits 16083
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础实战-数据库的操作之Mysqli与PDO(2018年8月30日)
大白鲸的博客
Original
703 people have browsed it

作业1:


作业2:


作业3:PDO连接数据库

实例

<?php
/**
 * Created by PhpStorm.
 * User: BIG-MAX
 * Date: 2018-08-31
 * Time: 8:55
 */
header('Content-type:text/html;charset=utf-8');

$dsn = 'mysql:host=127.0.0.1; dbname=oa_infomation';
$user = 'root';
$pass = 'root';
//实例化PDO类,创建pdo对象,
try{
    $stmt = new pdo($dsn, $user, $pass);
    //echo '<h2>连接成功!</h2>';

} catch (PDOException $e) {
    die('Connect ERROR! :' . $e->getMessage());
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

作业3:PDO新增记录操作

实例

<?php
header('Content-type:text/html;charset=utf-8');

/*新增数据*/
//连接数据库
$pdo = new PDO('mysql:host=127.0.0.1;dbname=oa_infomation' , 'root' , 'root');

//$pdo = new pdo('mysql:host=127.0.0.1;port=3306;dbname=oa_infomation;charset=utf8','root','root',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,));
//准备SQL语句,占位符我们不再用?,用“命名占位符”
//$sql = "INSERT 'use_unit' SET 'use_unit' = :use_unit , 'use_class' = :use_class";
$pdo->query("SET NAMES utf8");
$sql = "INSERT `use_unit` SET `use_unit`= :use_unit , `use_class`= :use_class";

//创建预处理对象
$stmt = $pdo->prepare($sql);

$data = ['use_unit'=>'信息档案中心','use_class'=>'三级单位'];
$stmt->bindParam(':use_unit',$data['use_unit'],PDO::PARAM_STR);
$stmt->bindParam(':use_class',$data['use_class'],PDO::PARAM_STR);

if($stmt->execute()){
//rowCount():返回受影响的记录数量	
	echo '<h3>成功添加了' .$stmt->rowCount(). '条记录</h3>';

}else {
	echo '<h3>添加失败</h3>';
	print_r($stmt->errorInfo());
	exit();
}
$stmt = null;
//关闭连接
$pdo = null;
//echo $stmt->queryString;exit;

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
椿 2018-08-31 18:16:53
1 floor
Author's latest blog post