PDO链接方法

时间:2022-10-04 17:17:26
<?php

//定义数据源
$dsn = "mysql:dbname=mydb;host=localhost";
//$dsn = "sqlsrv:dbname=mydb;host=localhost"; //造pdo对象
$pdo = new PDO($dsn,"root","123"); //写SQL语句
$sql = "select * from Info"; //准备执行SQL语句
$st = $pdo->prepare($sql); //执行预处理语句
if($st->execute())
{
/*while($attr = $st->fetch())
{
print_r($attr);
echo "<br>";
}*/ //从结果集中取所有数据,返回二维数组
//print_r($st->fetchAll(PDO::FETCH_NUM)); //从结果集中取一条数据中的某一列,返回字符串
//var_dump($st->fetchColumn(1)); //从结果集中取一条数据,返回一个实体类的对象
//var_dump($st->fetchObject()); }
else
{
echo "执行失败!";
} ?>