Blogger Information
Blog 27
fans 1
comment 1
visits 21779
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作修改指定数据-PHP线上五期
哥特的博客
Original
1141 people have browsed it

总结:需要修改的字段需要用单引号包裹的时候,一定要用斜杠进行转译。如果发现有修改不了的时候可以看看是不是已经是需要修改的数据和目标数据一样,或者在数据命令拼装好之后打印一下看看。


实例

<?php
header("Content-type: text/html; charset=utf-8");
	function con(){
		$dsn = 'mysql:host=127.0.0.1;dbname=a1;charset=utf8;port=3306';
		$dbname = 'root';
		$dbpw = 'root';
		try{
			$p = new PDO($dsn,$dbname,$dbpw);
		}catch(PDOException $e){
			print_r($e->getMessage());
			exit;
		}
		return $p;
	}
	function update_user($table,$name,$where){
		$c = con(); 
		$sql = 'UPDATE ';
		$sql .= $table;
		$sql .= ' SET ';
		$sql .= $name;
		if(!empty($where)){
			$sql .= ' WHERE ';
			$sql .= $where;
		}
		  // echo $sql;exit;
		$a = $c->prepare($sql);
		if($a->execute()){
			if($a->rowCount()){
				$a->setFetchMode(PDO::FETCH_ASSOC);
				$set = $a->fetchALL(); 
				return $set;
			}else{
				return false;
			}
			
		}else{
			return false;
		}
	}
	$s  =update_user('staff','`name`=\'张学友\'','id = 2');
	  var_dump($s);
	if($s!=0){
		echo '修改成功';
	} else{
		echo '修改失败';
	}
	
?>

运行实例 »

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


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