摘要:數(shù)據(jù)表:增加數(shù)據(jù)操作:<?php require 'demo21.php'; //2、寫sql語句 $sql = "insert into user(name,sex,age,email,password,status,creat_time)values(:name,:sex,:age,:email,:passwor
數(shù)據(jù)表:
增加數(shù)據(jù)操作:
<?php require 'demo21.php'; //2、寫sql語句 $sql = "insert into user(name,sex,age,email,password,status,creat_time)values(:name,:sex,:age,:email,:password,:status,:creat_time)"; //3驗(yàn)證sql語句 $stmt = $pdo->prepare($sql); //4、參數(shù)綁定 $name = '小王'; $sex = '1'; $age = '26'; $email = '403280636@qq.com'; $password = sha1('123456'); $status = '1'; $creatTime = time(); $stmt->bindParam(':name', $name, PDO::PARAM_STR, 20); $stmt->bindParam(':sex', $sex, PDO::PARAM_INT, 2); $stmt->bindParam(':age', $age, PDO::PARAM_INT, 4); $stmt->bindParam(':email', $email, PDO::PARAM_STR, 50); $stmt->bindParam(':password', $password, PDO::PARAM_STR, 100); $stmt->bindParam(':status', $status, PDO::PARAM_INT, 1); $stmt->bindParam(':creat_time', $creatTime, PDO::PARAM_INT, 15); //5.執(zhí)行添加 if ($stmt->execute()) { echo ($stmt->rowCount() > 0) ? '添加成功' : '添加失敗'; } else { exit(print_r($stmt->errorInfo(),true).'添加失敗'); }
刪除數(shù)據(jù)操作:
<?php require 'demo21.php'; //2、寫sql語句 $sql = "DELETE from user where id=:id "; //3驗(yàn)證sql語句 $stmt = $pdo->prepare($sql); //4、參數(shù)綁定 $id=11; $creatTime = time(); $stmt->bindParam(':id', $id, PDO::PARAM_INT, 2); //5.執(zhí)行添加 if ($stmt->execute()) { echo ($stmt->rowCount() > 0) ? '刪除成功' : '刪除失敗'.'成功刪除了'.$stmt->rowCount().'條數(shù)據(jù)'; } else { exit(print_r($stmt->errorInfo(),true).'刪除失敗'); }
修改數(shù)據(jù)操作:
<?php require 'demo21.php'; //2、寫sql語句 $sql = "UPDATE user set email=:email,creat_time=:creat_time where id=:id"; //3驗(yàn)證sql語句 $stmt = $pdo->prepare($sql); //4、參數(shù)綁定 $id=2; $email = '578869763@qq.com'; $creatTime = time(); $stmt->bindParam(':id', $id, PDO::PARAM_INT, 2); $stmt->bindParam(':email', $email, PDO::PARAM_STR, 50); $stmt->bindParam(':creat_time', $creatTime, PDO::PARAM_INT, 15); //5.執(zhí)行添加 if ($stmt->execute()) { echo ($stmt->rowCount() > 0) ? '修改成功' : '添加失敗'.'成功修改了'.$stmt->rowCount().'條數(shù)據(jù)'; } else { exit(print_r($stmt->errorInfo(),true).'修改失敗'); }
查詢數(shù)據(jù)操作:
<?php //require 'demo21.php'; try{ $pdo=new PDO('mysql:host=localhost;dbname=edu','root','root'); }catch (Exception $e){ exit($e->getMessage()); } $sql="select id,name,sex,age,creat_time from user where status=:status"; $stmt=$pdo->prepare($sql); $stmt->execute([':status'=>1]); $result=$stmt->fetchAll(PDO::FETCH_ASSOC); ?> <style> table{width: 600px;border: #ccc solid;border-spacing:0px;} table td{width:20%;white-space:nowrap;height: 30px;line-height: 30px;text-align: center;border: 1px solid #ccc} table tr:nth-child(odd){background-color: #9acfea} </style> <table cellpadding="0" cellspacing="0"> <tr> <th>id</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>注冊時間</th> </tr> <?php foreach($result as $k=>$v): ?> <tr> <td><?php echo $v['id'] ?></td> <td><?php echo $v['name'] ?></td> <td><?php echo $v['sex'] ?></td> <td><?php echo $v['age'] ?></td> <td><?php echo date('Y-h-d',$v['creat_time'])?></td> </tr> <?php endforeach; ?> </table>
批改老師:天蓬老師批改時間:2019-02-18 13:48:42
老師總結(jié):如果參數(shù)較多, 并且并不是從外部傳入的話, 你這樣寫有點(diǎn)麻煩:
$name = '小王';
$sex = '1';
$age = '26';
$email = '403280636@qq.com';
$password = sha1('123456');
$status = '1';
$creatTime = time();
$stmt->bindParam(':name', $name,