首页 > PHP开发 > pdo fetch,pdo fetchAll,pdo query,pdo prepare使用 – pdo笔记三

pdo fetch,pdo fetchAll,pdo query,pdo prepare使用 – pdo笔记三

通过pdo来从数据库中调取数据。
其中主要涉及到 pdo fetch,pdo fetchAll,pdo query,pdo prepare使用 。
可以通过以下几种方式。
第一、

$dbconn = array(
	'dns'=>"mysql:host=localhost;dbname=gosoa",
	'dbuser'=>'root',
	'dbpwd'=>'123456'
);
try{
	$db = new PDO($dbconn['dns'],$dbconn['dbuser'],$dbconn['dbpwd']);
	$db->query("set names utf8");
	$sql = "SELECT * FROM wp_posts  ";
	$query = $db->query($sql);
	foreach($query as $rs)
	{
		print_r($rs);
	}
}catch(PDOException  $e)
{
	echo $e->getMessage();
}

第二种、

try{
	$db = new PDO($dbconn['dns'],$dbconn['dbuser'],$dbconn['dbpwd']);
	$db->query("set names utf8");
	$sql = "SELECT * FROM wp_posts limit 2";
	$query = $db->query($sql);
	$result = $query->fetchAll();
	print_r($result);

}catch(PDOException  $e)
{
	echo $e->getMessage();
}

第三种

try{
	$db = new PDO($dbconn['dns'],$dbconn['dbuser'],$dbconn['dbpwd']);
	$db->query("set names utf8");
	$sql = "SELECT * FROM wp_posts limit 2";
	$query = $db->query($sql);
	$result = $query->fetch();
	foreach($query as $rs)
	{
		print_r($rs);
	}

}catch(PDOException  $e)
{
	echo $e->getMessage();
}

第四种

try{
	$db = new PDO($dbconn['dns'],$dbconn['dbuser'],$dbconn['dbpwd']);
	$db->query("set names utf8");
	$sql = "SELECT * FROM wp_posts where ID = :id ";
	$sth = $db->prepare($sql);
	$sth->execute(array(':id'=>'12'));
	$rs = $sth->fetchAll();
	print_r($rs);

}catch(PDOException  $e)
{
	echo $e->getMessage();
}

pdo fetch,pdo fetchAll,pdo query,pdo prepare使用 基本的就这些。

其他很详细的信息,去查 php手册吧。

呵呵。

  1. 2010年6月7日07:04 | #1

    Wow this is a great resource.. I’m enjoying it.. good article

  1. 本文目前尚无任何 trackbacks 和 pingbacks.