<script>ec(2);</script>
【PDO配置】
1、确保PHP版本为5.2.5以上(主要是我用的5.2.5,第一次不知道用的5.1.x的,结果一直搞不好~_~)
2、在php.ini中找到Dynamic Extensions扩展部分,去掉extension=php_pdo.dll前面的分号
3、去掉相应数据库PDO扩展前面的分号,如:extension=php_pdo_mysql.dll
【范例中数据库】
CREATE TABLE tablename (
id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
str varchar(50) NOT NULL DEFAULT '''',
PRIMARY KEY (id)
);
【程序范例】
/*
* 数据库配置信息
*/
$dsn = "mysql:host=localhost;dbname=test";
$user = ''root'';
$passwd = ''123456'';
/*
* 链接数据库,并测试是否链接成功
*/
try{
$db = new PDO($dsn, $user, $passwd);
}catch (PDOException $e)
{
echo "链接数据库失败!";
print "异常信息: ". $e->getMessage() . "
";
print "异常文件: " . $e->getFile() . "
";
print "异常行号: " . $e->getLine() . "
";
exit();
}
/*
* 插入
*/
//$sql = "INSERT INTO tablename SET str = ''Hello''";
//$count = $db->exec($sql); //返回值为影响的行数
/*
* 删除
*/
//$sql = "DELETE FROM tablename WHERE str = ''Hello'' LIMIT 1";
//$count = $db->exec($sql); //返回值为影响的行数
/*
* 查询
*/
//预处理需要查询的SQL语句
//$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); //列名按照原始的方式(字段)
$sql = "SELECT * FROM tablename WHERE id $query = $db->prepare($sql); //预处理
//用一组绑定参数执行一遍查询
$query->execute(array('':id''=>1, '':string''=>''Hello'')); //处理语句(参数绑定方式)
//$query->setFetchMode(PDO::FETCH_ASSOC); 关联数组形式(只通过字段名下标访问数组内容)
while($item =
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn