标签:blog io ar os for 数据 on div log
<?php
/**
* 声明一个数组存放数据库链接信息
*/
$C = array();
/**
* 在数据库链接信息数组中存放相关信息
*/
$C[‘DB_HOST‘] = ‘localhost‘;
$C[‘DB_USER‘] = ‘root‘;
$C[‘DB_PASS‘] = ‘123456‘;
$C[‘DB_NAME‘] = ‘db_test‘;
/**
* 循环生成常量
*/
foreach ($C as $name => $val) {
define($name , $val);
}
/**
* 用PDO链接数据库
*/
$dbo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . "","" . DB_USER . "","" . DB_PASS . "") or die("数据库链接失败");
/**
* 试着写一个链接数据库的例子
*/
try{
$rs = $dbo -> prepare("select * from `test`");
$rs->execute();
$results = $rs->FETCHALL(PDO::FETCH_ASSOC);
$dbo = null;
print_r($results);
}
catch( Exception $e)
{
print $e->getMessage();
exit;
}
/*End of file DB.class.php*/
标签:blog io ar os for 数据 on div log
原文地址:http://www.cnblogs.com/peter767/p/4118057.html