search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP5 PDO connection to database_PHP tutorial
Detailed explanation of PHP5 PDO connection to database_PHP tutorialJul 13, 2016 pm 05:41 PM
datapdophpphp5introducedatabaseIntroductionDetailed explanationconnect

This article introduces PHP5 PDO to connect to the database
1. Introduction to PDO
PDO (PHP Data Object) is something added in PHP 5. It is a major new feature added in PHP 5, because in PHP 5 In the past, php4/php3 had a bunch of database extensions to connect and process each database, such as php_mysql.dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll, etc.
PHP6 will also use PDO to connect by default, and the mysql extension will be used as an auxiliary

2. PDO configuration
In PHP.ini, remove "extension=php_pdo. dll". If you want to connect to the database, you also need to remove the ";" sign in front of the database extension related to PDO, and then restart the Apache server.
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_mssql.dll
extension=php_pdo_odbc.dll
extension=php_pdo_firebird.dll
......

3. PDO connects to mysql database
new PDO("mysql:host=localhost;dbname=db_demo" ,"root","");
The default is not a long connection. If you want to use a long connection to the database, you need to add the following parameters at the end:
new PDO("mysql:host=localhost;dbname=db_demo"," root","","array(PDO::ATTR_PERSISTENT => true) ");

4. Common PDO methods and their applications
PDO::query() Mainly used for operations that return recorded results, especially SELECT operations
PDO::exec() Mainly used for operations that do not return a result set, such as INSERT, UPDATE and other operations
PDO::lastInsertId() returns In the last insertion operation, the primary key column type is the last auto-incremented ID
PDOStatement::fetch() is used to get a record
PDOStatement::fetchAll() is used to get all the record sets into one

5. PDO operates MYSQL database instance
$pdo = new PDO("mysql:host=localhost;dbname=db_demo","root", "");
if($pdo -> exec("insert into db_demo(name,content) values(title,content)")){
echo "Insertion successful!";
echo $ pdo -> lastinsertid();
}
?>
$pdo = new PDO("mysql:host=localhost;dbname=db_demo","root", "");
$rs = $pdo -> query("select * from test");
while($row = $rs -> fetch()){
print_r($row );
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486143.htmlTechArticleThis article introduces PHP5 PDO to connect to the database 1. Introduction to PDO PDO (PHP Data Object) is something added in PHP 5. It is a major new feature added to PHP 5, because before PHP 5, php4/php3 were all...
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
PHP Fatal error: Call to undefined method PDO::prepare() in的解决方法PHP Fatal error: Call to undefined method PDO::prepare() in的解决方法Jun 22, 2023 pm 06:40 PM

PHP作为一种流行的Web开发语言,已经被使用了很长时间。PHP中集成的PDO(PHP数据对象)类是我们在开发Web应用程序过程中与数据库进行交互的一种常用方法。但是,一些PHP开发者经常遇到的问题是,当使用PDO类与数据库进行交互时,他们会收到这样的错误:PHPFatalerror:CalltoundefinedmethodPDO::prep

php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

php如何使用PHP的PDO_PGSQL扩展?php如何使用PHP的PDO_PGSQL扩展?Jun 02, 2023 pm 06:10 PM

PHP作为一种流行的编程语言,在Web开发领域中有着广泛的应用。其中,PHP的PDO_PGSQL扩展是一种常用的PHP扩展,它提供了与PostgreSQL数据库的交互接口,可以实现PHP与PostgreSQL之间的数据传输和交互。本文将详细介绍如何使用PHP的PDO_PGSQL扩展。一、什么是PDO_PGSQL扩展?PDO_PGSQL是PHP的一个扩展库,它

PHP和PDO: 如何执行批量插入和更新PHP和PDO: 如何执行批量插入和更新Jul 28, 2023 pm 07:41 PM

PHP和PDO:如何执行批量插入和更新导言:在使用PHP编写数据库相关的应用程序时,经常会遇到需要批量插入和更新数据的情况。传统的做法是使用循环来执行多次数据库操作,但这样的方法效率较低。PHP的PDO(PHPDataObject)提供了一种更高效的方法来执行批量插入和更新操作,本文将介绍如何使用PDO来实现批量插入和更新。一、PDO简介:PDO是PH

如何使用PDO连接到Redis数据库如何使用PDO连接到Redis数据库Jul 28, 2023 pm 04:29 PM

如何使用PDO连接到Redis数据库Redis是一个开源的高性能、内存存储的键值数据库,常用于缓存、队列等场景。在PHP开发中,使用Redis可以有效提升应用的性能和稳定性。而通过PDO(PHPDataObjects)扩展,我们可以更方便地连接和操作Redis数据库。本文将介绍如何使用PDO连接到Redis数据库,并附带代码示例。安装Redis扩展在开始

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

如何使用PDO绑定和获取绑定参数值如何使用PDO绑定和获取绑定参数值Jul 28, 2023 pm 07:09 PM

如何使用PDO绑定和获取绑定参数值在开发Web应用程序时,处理数据库查询是很常见的任务之一。为了保证应用程序的安全性和可靠性,我们应该使用参数绑定来处理SQL查询,而不是直接将变量值插入SQL语句中。PDO(PHP数据对象)提供了一种方便且安全的方式来绑定参数和获取绑定参数的值。下面,我们将介绍如何使用PDO进行参数绑定和获取绑定参数的

使用PDO进行数据库操作:PHP的一个更好的方式使用PDO进行数据库操作:PHP的一个更好的方式Jun 21, 2023 pm 01:36 PM

使用PDO进行数据库操作:PHP的一个更好的方式在Web开发中,使用数据库进行数据存储、管理和查询是非常常见的。而PHP作为一种广泛应用于Web开发的语言,自然也提供了丰富的数据库操作方式。在PHP中,可以使用MySQLi、PDO以及其他扩展库来进行数据库操作。其中,PDO是一种非常常用的数据库操作方式,相比于其他方式有更多的优点。本文将介绍什么是PDO,以

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!