Blogger Information
Blog 61
fans 0
comment 0
visits 54056
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO练习2
笑颜常开的博客
Original
627 people have browsed it

<?php
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
$sql='insert into `staff`(`name`,`age`,`sex`,`position`,`mobile`,`hiredate`)';
$sql.='values(:name,:age,:sex,:position,:mobile,:hiredate)';
$stmt=$pdo->prepare($sql);
//参数绑定
//$name='孙连城';
//$age=48;
//$sex=1;
//$position='光明区长';
//$mobile='18607093087';
//$hiredate=time();
//$stmt->bindParam(':name', $name, PDO::PARAM_STR, 20);
//$stmt->bindParam(':age', $age, PDO::PARAM_INT);
//$stmt->bindParam(':sex', $sex, PDO::PARAM_INT);
//$stmt->bindParam(':position', $position, PDO::PARAM_STR, 20);
//$stmt->bindParam(':mobile', $mobile, PDO::PARAM_STR, 11);
//$stmt->bindParam(':hiredate', $hiredate, PDO::PARAM_INT);
////执行sql语句
//$stmt->execute();
//if($stmt->rowCount()>0){
//    echo '成功地添加了'.$stmt->rowCount().'条记录';
//}
////3.关闭连接
//$pdo=null;
$key = [':name', ':age', ':sex', ':position', ':mobile', ':hiredate'];
$value = ['蔡成功', 42, 1, '大风厂长', '1358999321', time()];
// array_combine()将键数组与值数组合并成新数组
$data = array_combine($key, $value);

echo '<pre>'.print_r($data,true);
// 执行
// execute()参数数组值,默认全部字符类型,要注意
$stmt->execute($data);

// 4. 对执行结果进行处理
// rowCount()返回受影响的记录数量
if ($stmt->rowCount() > 0) {
   echo '成功的添加了' . $stmt->rowCount() .  '条记录';
}

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