search
Homephp教程PHP源码PHP5.2中PDO的简单使用

<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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment