Blogger Information
Blog 35
fans 2
comment 0
visits 22732
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
面向对象更新和PDO删除---2018年4月26
小学僧的博客
Original
728 people have browsed it

1.mysqli面向对象实现更新:

实例

<?php
require 'mysqli_con.php';

$sql = "UPDATE IGNORE staff SET age = ?, salary = ? WHERE staff_id = ?;";

$stmt = $mysqli->stmt_init();	//创建对象
//$stmt = mysqli_stmt_init($db);	

if($stmt->prepare($sql)){
	$data[] = ['age'=>'42','salary'=>'6000','staff_id'=>'2'];
	$data[] = ['age'=>'30','salary'=>'5000','staff_id'=>'6'];
	
	$stmt->bind_param('iii',$age,$salary,$staff_id);
	foreach($data as $staff){
		$age = $staff["age"];
		$salary = $staff["salary"];
		$staff_id = $staff["staff_id"];
		$stmt->execute();
		if($stmt->affected_rows > 0){
			echo 'new update:'.$stmt->affected_rows;
		}else{
			echo 'no update';
		}			
	}
		
	$stmt->close();
}else{
	exit($stmt->errno().':'.$stmt->error());
}

$mysqli->close();

运行实例 »

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

2.PDO实现删除

实例

<?php

//1.连接数据库,创建pdo对象
$pdo = new PDO('mysql:dbname=php','root','root');

//2.创建预处理语句对象
$stmt = $pdo->prepare("DELETE  FROM  staff  WHERE staff_id=:staff_id");

//3.将参数绑定到预处理语句对象并执行
$stmt -> execute(['staff_id'=>6]);

echo 'new delete:'.$stmt->rowCount();

运行实例 »

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


Correction status:Uncorrected

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
0 comments
Author's latest blog post