PDO手记2

<?php
$dsn = 'mysql:dbname=test;host=127.0.0.1';
try{
$dbh= new PDO($dsn, 'root', '123456');
} catch(PDOException $e){
echo "数据库连接失败:".$e->getMessage();
}
$dbh->exec( "set names gbk");
$count = $dbh->exec( "INSERT INTO student(name,age,major) values('张三',23,'工业设计')");

echo "插入".$count. "行记录";
$db = null;
?>
 
 
测试用的表
 
 
<?php
$dsn = 'mysql:dbname=test;host=127.0.0.1';
try{
$dbh= new PDO($dsn, 'root', '123456');
} catch(PDOException $e){
echo "数据库连接失败:".$e->getMessage();
}
$dbh->exec( "set names gbk");
$rs=$dbh->query( "SELECT * FROM student");
while($row=$rs->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
}

$db = null;
?>

你可能感兴趣的:(PHP,职场,pdo,休闲)