搜索
首页后端开发php教程php pdo函数库用法详解

  1. pdo->begintransaction() — 标明回滚起始点
  2. pdo->commit() — 标明回滚结束点,并执行sql
  3. pdo->__construct() — 建立一个pdo链接数据库的实例
  4. pdo->errorcode() — 获取错误码
  5. pdo->errorinfo() — 获取错误的信息
  6. pdo->exec() — 处理一条sql语句,并返回所影响的条目数
  7. pdo->getattribute() — 获取一个“数据库连接对象”的属性
  8. pdo->getavailabledrivers() — 获取有效的pdo驱动器名称
  9. pdo->lastinsertid() — 获取写入的最后一条数据的主键值
  10. pdo->prepare() — 生成一个“查询对象”
  11. pdo->query() — 处理一条sql语句,并返回一个“pdostatement”
  12. pdo->quote() — 为某个sql中的字符串添加引号
  13. pdo->rollback() — 执行回滚
  14. pdo->setattribute() — 为一个“数据库连接对象”设定属性
复制代码

二、pdostatement

  1. pdostatement->bindcolumn() — bind a column to a php variable
  2. pdostatement->bindparam() — binds a parameter to the specified variable name
  3. pdostatement->bindvalue() — binds a value to a parameter
  4. pdostatement->closecursor() — closes the cursor, enabling the statement to be executed again.
  5. pdostatement->columncount() — returns the number of columns in the result set
  6. pdostatement->errorcode() — fetch the sqlstate associated with the last operation on the statement handle
  7. pdostatement->errorinfo() — fetch extended error information associated with the last operation on the statement handle
  8. pdostatement->execute() — executes a prepared statement
  9. pdostatement->fetch() — fetches the next row from a result set
  10. pdostatement->fetchall() — returns an array containing all of the result set rows
  11. pdostatement->fetchcolumn() — returns a single column from the next row of a result set
  12. pdostatement->fetchobject() — fetches the next row and returns it as an object.
  13. pdostatement->getattribute() — retrieve a statement attribute
  14. pdostatement->getcolumnmeta() — returns metadata for a column in a result set
  15. pdostatement->nextrowset() — advances to the next rowset in a multi-rowset statement handle
  16. pdostatement->rowcount() — returns the number of rows affected by the last sql statement
  17. pdostatement->setattribute() — set a statement attribute
  18. pdostatement->setfetchmode() — set the default fetch mode for this statement
复制代码

详解1) pdo中的数据库连接

  1. $dsn = ‘mysql:dbname=ent;host=127.0.0.1′;
  2. $user = ‘root';
  3. $password = ‘123456′;
  4. try {
  5. $dbh = new pdo($dsn, $user, $password, array(pdo::attr_persistent => true));
  6. $dbh->query('set names utf8;');
  7. foreach ($dbh->query('select * from tpm_juese') as $row) {
  8. print_r($row);
  9. }
  10. } catch (pdoexception $e) {
  11. echo ‘connection failed: ‘ . $e->getmessage();
  12. }
复制代码

许多web应用会因为使用了向数据库的持久连接而得到优化。持久连接不会在脚本结束时关闭, 相反它会被缓存起来并在另一个脚本通过同样的标识请求一个连接时得以重新利用。 持久连接的缓存可以使你避免在脚本每次需要与数据库对话时都要部署一个新的连接的资源消耗,让你的web应用更加快速。 上面实例中的array(pdo::attr_persistent => true)就是把连接类型设置为持久连接。

详解2) pdo中的事务 pdo->begintransaction(),pdo->commit(),pdo->rollback()这三个方法是在支持回滚功能时一起使用的。 pdo->begintransaction()方法标明起始点,pdo->commit()方法标明回滚结束点,并执行sql,pdo->rollback()执行回滚。

  1. try {
  2. $dbh = new pdo('mysql:host=localhost;dbname=test', ‘root', ”);
  3. $dbh->query('set names utf8;');
  4. $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);
  5. $dbh->begintransaction();
  6. $dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('mick', 22);”);
  7. $dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('lily', 29);”);
  8. $dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('susan', 21);”);
  9. $dbh->commit();
  10. } catch (exception $e) {
  11. $dbh->rollback();
  12. echo “failed: ” . $e->getmessage();
  13. }
  14. ?>
复制代码

现在已经通过pdo建立了连接,在部署查询之前你必须搞明白pdo是怎样管理事务的。如果你以前从未遇到过事务处理,(现在简单介绍一下:)它们提供了4个主要的特性:原子性,一致性,独立性和持久性(atomicity, consistency, isolation and durability,acid)通俗一点讲,一个事务中所有的工作在提交时,即使它是分阶段执行的,也要保证安全地应用于数据库,不被其他的连接干扰。事务工作也可以在请求发生错误时轻松地自动取消。

事务的典型运用就是通过把批量的改变“保存起来”然后立即执行。这样就会有彻底地提高更新效率的好处。换句话说,事务可以使你的脚本更快速同时可能更健壮(要实现这个优点你仍然需要正确的使用它们)。

不幸运的是,并不是每个数据库都支持事务,因此pdo需要在建立连接时运行在被认为是“自动提交”的模式下。自动提交模式意味着你执行的每个查询都有它自己隐含的事务处理,无论数据库支持事务还是因数据库不支持而不存在事务。如果你需要一个事务,你必须使用 pdo->begintransaction() 方法创建一个。如果底层驱动不支持事务处理,一个pdoexception就会被抛出(与你的异常处理设置无关,因为这总是一个严重的错误状态)。在一个事物中,你可以使用 pdo->commit() 或 pdo->rollback() 结束它,这取决于事务中代码运行是否成功。 当脚本结束时或一个连接要关闭时,如果你还有一个未处理完的事务,pdo将会自动将其回滚。这是对于脚本意外终止的情况来说是一个安全的方案——如果你没有明确地提交事务,它将会假设发生了一些错误,为了你数据的安全,所以就执行回滚了。

二、pdostatement

  1. // 修改默认的错误显示级别
  2. $dbh->setattribute(pdo::attr_errmode, pdo::errmode_warning);
  3. ?>
复制代码

属性列表: pdo::param_bool 表示一个布尔类型 pdo::param_null 表示一个sql中的null类型 pdo::param_int 表示一个sql中的integer类型 pdo::param_str 表示一个sql中的sql char,varchar类型 pdo::param_lob 表示一个sql中的large object类型 pdo::param_stmt 表示一个sql中的recordset类型,还没有被支持 pdo::param_input_output specifies that the parameter is an inout parameter for a stored procedure. you must bitwise-or this value with an explicit pdo::param_* data type. pdo::fetch_lazy 将每一行结果作为一个对象返回 pdo::fetch_assoc 仅仅返回以键值作为下标的查询的结果集,名称相同的数据只返回一个 pdo::fetch_named 仅仅返回以键值作为下标的查询的结果集,名称相同的数据以数组形式返回 pdo::fetch_num 仅仅返回以数字作为下标的查询的结果集 pdo::fetch_both 同时返回以键值和数字作为下标的查询的结果集 pdo::fetch_obj 以对象的形式返回结果集 pdo::fetch_bound 将pdostatement::bindparam()和pdostatement::bindcolumn()所绑定的值作为变量名赋值后返回 pdo::fetch_column 表示仅仅返回结果集中的某一列 pdo::fetch_class 表示以类的形式返回结果集 pdo::fetch_into 表示将数据合并入一个存在的类中进行返回 pdo::fetch_func pdo::fetch_group pdo::fetch_unique pdo::fetch_key_pair 以首个键值下表,后面数字下表的形式返回结果集 pdo::fetch_classtype pdo::fetch_serialize 表示将数据合并入一个存在的类中并序列化返回 pdo::fetch_props_late available since php 5.2.0 pdo::attr_autocommit 在设置成true的时候,pdo会自动尝试停止接受委托,开始执行 pdo::attr_prefetch 设置应用程序提前获取的数据大小,并非所有的数据库哦度支持 pdo::attr_timeout 设置连接数据库超时的值 pdo::attr_errmode 设置error处理的模式 pdo::attr_server_version 只读属性,表示pdo连接的服务器端数据库版本 pdo::attr_client_version 只读属性,表示pdo连接的客户端pdo驱动版本 pdo::attr_server_info 只读属性,表示pdo连接的服务器的meta信息 pdo::attr_connection_status pdo::attr_case 通过pdo::case_*中的内容对列的形式进行操作 pdo::attr_cursor_name 获取或者设定指针的名称 pdo::attr_cursor 设置指针的类型,pdo现在支持pdo::cursor_fwdonly和pdo::cursor_fwdonly pdo::attr_driver_name 返回使用的pdo驱动的名称 pdo::attr_oracle_nulls 将返回的空字符串转换为sql的null pdo::attr_persistent 获取一个存在的连接 pdo::attr_statement_class pdo::attr_fetch_catalog_names 在返回的结果集中,使用自定义目录名称来代替字段名。 pdo::attr_fetch_table_names 在返回的结果集中,使用自定义表格名称来代替字段名。 pdo::attr_stringify_fetches pdo::attr_max_column_len pdo::attr_default_fetch_mode available since php 5.2.0 pdo::attr_emulate_prepares available since php 5.1.3. pdo::errmode_silent 发生错误时不汇报任何的错误信息,是默认值 pdo::errmode_warning 发生错误时发出一条php的e_warning的信息 pdo::errmode_exception 发生错误时抛出一个pdoexception pdo::case_natural 回复列的默认显示格式 pdo::case_lower 强制列的名字小写 pdo::case_upper 强制列的名字大写 pdo::null_natural pdo::null_empty_string pdo::null_to_string pdo::fetch_ori_next 获取结果集中的下一行数据,仅在有指针功能时有效 pdo::fetch_ori_prior 获取结果集中的上一行数据,仅在有指针功能时有效 pdo::fetch_ori_first 获取结果集中的第一行数据,仅在有指针功能时有效 pdo::fetch_ori_last 获取结果集中的最后一行数据,仅在有指针功能时有效 pdo::fetch_ori_abs 获取结果集中的某一行数据,仅在有指针功能时有效 pdo::fetch_ori_rel 获取结果集中当前行后某行的数据,仅在有指针功能时有效 pdo::cursor_fwdonly 建立一个只能向后的指针操作对象 pdo::cursor_scroll 建立一个指针操作对象,传递pdo::fetch_ori_*中的内容来控制结果集 pdo::err_none (string)

设定没有错误时候的错误信息 pdo::param_evt_alloc allocation event pdo::param_evt_free deallocation event pdo::param_evt_exec_pre event triggered prior to execution of a prepared statement. pdo::param_evt_exec_post event triggered subsequent to execution of a prepared statement. pdo::param_evt_fetch_pre event triggered prior to fetching a result from a resultset. pdo::param_evt_fetch_post event triggered subsequent to fetching a result from a resultset. pdo::param_evt_normalize event triggered during bound parameter registration allowing the driver to normalize the parameter name.



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
超越炒作:评估当今PHP的角色超越炒作:评估当今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

PHP中的弱参考是什么?什么时候有用?PHP中的弱参考是什么?什么时候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

解释PHP中的__ Invoke Magic方法。解释PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

解释PHP 8.1中的纤维以进行并发。解释PHP 8.1中的纤维以进行并发。Apr 12, 2025 am 12:05 AM

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区:资源,支持和发展PHP社区:资源,支持和发展Apr 12, 2025 am 12:04 AM

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP与Python:了解差异PHP与Python:了解差异Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

php:死亡还是简单地适应?php:死亡还是简单地适应?Apr 11, 2025 am 12:13 AM

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来:改编和创新PHP的未来:改编和创新Apr 11, 2025 am 12:01 AM

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器