Blogger Information
Blog 29
fans 0
comment 0
visits 29277
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO预处理查询操作
咸鱼梦
Original
1542 people have browsed it
<?php

try {
	$dsn = 'mysql:dbname=demo';
	$userName = 'root';
	$password = 'root';
	$pdo = new PDO($dsn,$userName,$password);
	
	//创建预处理sql语句
	$sql = "SELECT `id`,`name`,`email` FROM `user` WHERE `id`>:id";
	//执行预处理sql语句,创建一个PDOStatement
	$pdoStmt = $pdo->prepare($sql);
	//判断$pdoStmt对象是否创建成功
	if ($pdoStmt == true) {
		//执行预处理语句:execute()
		$res = $pdoStmt->execute(['id'=>5]);
		if ($res == true) {
			if ($pdoStmt->rowCount() > 0) { //rowCount()返回当前有多少条数据
				//第一种: foreach()
				/*foreach ($pdoStmt as $row) {
					echo '<pre>';
					print_r($row);
				}*/
				//第二种:fetchAll()
				/*$pdoStmt->setFetchMode(PDO::FETCH_ASSOC);
				$row = $pdoStmt->fetchAll();
				echo '<pre>';
				print_r($row);*/
				//第三种:while()
				while ($row = $pdoStmt->fetch(PDO::FETCH_ASSOC)) {
					echo '<pre>';
					print_r($row);
				}
			} else {
				echo '当前数据表里没有改数据';
			}
		} else {
			echo '当前数据表里没有改数据';
		}
	} else {
		print_r($pdo->errorInfo());//创建失败返回错误语句
	}
	
	
	
} catch (PDOException $e) {
	print 'Connect ERROR'.$e->getMessage();
	dre();
}


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