pdo fetch,pdo fetchAll,pdo query,pdo prepare用法举例
通过pdo来从数据库中调取数据。
其中主要涉及到 pdo fetch,pdo fetchAll,pdo query,pdo prepare使用 。
可以通过以下几种方式,学习下php pdo的用法。
第一、
'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手册吧。
您可能感兴趣的文章:
pdo fetch,pdo fetchAll,pdo query,pdo prepare用法举例
php pdo函数库用法详解
PDO与MySQLi:PHP数据库API之战
pdo 调用mysql 存储过程
学习使用PDO查询Mysql来避免SQL注入风险
php5使用pdo连接数据库实例
php中PDO入门学习笔记
深入分析PHP PDO配置及使用方法
有关PHP中PDO连接数据库的详细教程与实际操作演示
PHP PDO数据库类select操作