通过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使用 基本的就这些。