Blogger Information
Blog 48
fans 0
comment 0
visits 36378
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
pdo新增数据-2018.08.30
雨天的博客
Original
532 people have browsed it

实例

<?php
/**
 * pdo新增数据
 */
//链接数据库
$dsn = 'mysql:host=localhost;dbname=data';
$username = 'root';
$pwd = 'root';
$pdo = new PDO($dsn,$username,$pwd);

//sql语句
$sql = "insert `stu` set `name`= :name,`address`= :address,`datetime`= :datetime";

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


//参数绑定
$data = ['name'=>'小龙女','address'=>'宋朝','datetime'=>time()];
//var_dump($data);
//exit;
$stmt->bindParam(':name',$data['name'],PDO::PARAM_STR);
$stmt->bindParam(':address',$data['address'],PDO::PARAM_STR);
$stmt->bindParam(':datetime',$data['datetime'],PDO::PARAM_INT);

//var_dump($stmt->queryString);
//exit;
//执行sql语句
if($stmt->execute())
{
    echo '成功添加了'.$stmt->rowCount().'条记录';
}else{
    echo '<h3>添加失败</h3>';
    print_r($stmt->errorInfo());
    exit();
}
$stmt = null;
$pdo = null;

运行实例 »

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


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
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!