复制代码
二、pdostatement
pdostatement->bindcolumn() — bind a column to a php variablepdostatement->bindparam() — binds a parameter to the specified variable namepdostatement->bindvalue() — binds a value to a parameterpdostatement->closecursor() — closes the cursor, enabling the statement to be executed again.pdostatement->columncount() — returns the number of columns in the result setpdostatement->errorcode() — fetch the sqlstate associated with the last operation on the statement handlepdostatement->errorinfo() — fetch extended error information associated with the last operation on the statement handlepdostatement->execute() — executes a prepared statementpdostatement->fetch() — fetches the next row from a result setpdostatement->fetchall() — returns an array containing all of the result set rowspdostatement->fetchcolumn() — returns a single column from the next row of a result setpdostatement->fetchobject() — fetches the next row and returns it as an object.pdostatement->getattribute() — retrieve a statement attributepdostatement->getcolumnmeta() — returns metadata for a column in a result setpdostatement->nextrowset() — advances to the next rowset in a multi-rowset statement handlepdostatement->rowcount() — returns the number of rows affected by the last sql statementpdostatement->setattribute() — set a statement attributepdostatement->setfetchmode() — set the default fetch mode for this statement
复制代码
详解1) pdo中的数据库连接
$dsn = ‘mysql:dbname=ent;host=127.0.0.1′;$user = ‘root';$password = ‘123456′;try {$dbh = new pdo($dsn, $user, $password, array(pdo::attr_persistent => true));$dbh->query('set names utf8;');foreach ($dbh->query('select * from tpm_juese') as $row) {print_r($row);}} catch (pdoexception $e) {echo ‘connection failed: ‘ . $e->getmessage();}
复制代码
许多web应用会因为使用了向数据库的持久连接而得到优化。持久连接不会在脚本结束时关闭,相反它会被缓存起来并在另一个脚本通过同样的标识请求一个连接时得以重新利用。持久连接的缓存可以使你避免在脚本每次需要与数据库对话时都要部署一个新的连接的资源消耗,让你的web应用更加快速。上面实例中的array(pdo::attr_persistent => true)就是把连接类型设置为持久连接。
详解2) pdo中的事务pdo->begintransaction(),pdo->commit(),pdo->rollback()这三个方法是在支持回滚功能时一起使用的。pdo->begintransaction()方法标明起始点,pdo->commit()方法标明回滚结束点,并执行sql,pdo->rollback()执行回滚。
query('set names utf8;');$dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);$dbh->begintransaction();$dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('mick', 22);”);$dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('lily', 29);”);$dbh->exec(”insert into `test`.`table` (`name` ,`age`)values ('susan', 21);”);$dbh->commit();} catch (exception $e) {$dbh->rollback();echo “failed: ” . $e->getmessage();}?>
复制代码
现在已经通过pdo建立了连接,在部署查询之前你必须搞明白pdo是怎样管理事务的。如果你以前从未遇到过事务处理,(现在简单介绍一下:)它们提供了4个主要的特性:原子性,一致性,独立性和持久性(atomicity, consistency, isolation and durability,acid)通俗一点讲,一个事务中所有的工作在提交时,即使它是分阶段执行的,也要保证安全地应用于数据库,不被其他的连接干扰。事务工作也可以在请求发生错误时轻松地自动取消。
事务的典型运用就是通过把批量的改变“保存起来”然后立即执行。这样就会有彻底地提高更新效率的好处。换句话说,事务可以使你的脚本更快速同时可能更健壮(要实现这个优点你仍然需要正确的使用它们)。
不幸运的是,并不是每个数据库都支持事务,因此pdo需要在建立连接时运行在被认为是“自动提交”的模式下。自动提交模式意味着你执行的每个查询都有它自己隐含的事务处理,无论数据库支持事务还是因数据库不支持而不存在事务。如果你需要一个事务,你必须使用 pdo->begintransaction() 方法创建一个。如果底层驱动不支持事务处理,一个pdoexception就会被抛出(与你的异常处理设置无关,因为这总是一个严重的错误状态)。在一个事物中,你可以使用 pdo->commit() 或 pdo->rollback() 结束它,这取决于事务中代码运行是否成功。当脚本结束时或一个连接要关闭时,如果你还有一个未处理完的事务,pdo将会自动将其回滚。这是对于脚本意外终止的情况来说是一个安全的方案——如果你没有明确地提交事务,它将会假设发生了一些错误,为了你数据的安全,所以就执行回滚了。
二、pdostatement
setattribute(pdo::attr_errmode, pdo::errmode_warning);?>
复制代码
属性列表: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_outputspecifies 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_funcpdo::fetch_grouppdo::fetch_uniquepdo::fetch_key_pair以首个键值下表,后面数字下表的形式返回结果集pdo::fetch_classtypepdo::fetch_serialize表示将数据合并入一个存在的类中并序列化返回pdo::fetch_props_lateavailable since php 5.2.0pdo::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_statuspdo::attr_case通过pdo::case_*中的内容对列的形式进行操作pdo::attr_cursor_name获取或者设定指针的名称pdo::attr_cursor设置指针的类型,pdo现在支持pdo::cursor_fwdonly和pdo::cursor_fwdonlypdo::attr_driver_name返回使用的pdo驱动的名称pdo::attr_oracle_nulls将返回的空字符串转换为sql的nullpdo::attr_persistent获取一个存在的连接pdo::attr_statement_classpdo::attr_fetch_catalog_names在返回的结果集中,使用自定义目录名称来代替字段名。pdo::attr_fetch_table_names在返回的结果集中,使用自定义表格名称来代替字段名。pdo::attr_stringify_fetchespdo::attr_max_column_lenpdo::attr_default_fetch_modeavailable since php 5.2.0pdo::attr_emulate_preparesavailable since php 5.1.3.pdo::errmode_silent发生错误时不汇报任何的错误信息,是默认值pdo::errmode_warning发生错误时发出一条php的e_warning的信息pdo::errmode_exception发生错误时抛出一个pdoexceptionpdo::case_natural回复列的默认显示格式pdo::case_lower强制列的名字小写pdo::case_upper强制列的名字大写pdo::null_naturalpdo::null_empty_stringpdo::null_to_stringpdo::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_allocallocation eventpdo::param_evt_freedeallocation eventpdo::param_evt_exec_preevent triggered prior to execution of a prepared statement.pdo::param_evt_exec_postevent triggered subsequent to execution of a prepared statement.pdo::param_evt_fetch_preevent triggered prior to fetching a result from a resultset.pdo::param_evt_fetch_postevent triggered subsequent to fetching a result from a resultset.pdo::param_evt_normalizeevent triggered during bound parameter registration allowing the driver to normalize the parameter name.
