使用方法设置预定义变量
PDO的方法/属性
- PDO::beginTransaction — Initiates a transaction
- PDO::commit — Commits a transaction
- PDO::__construct — Creates a PDO instance representing a connection to a database //构造方法
- PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
- PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle
- PDO::exec — Execute an SQL statement and return the number of affected rows
- PDO::getAttribute — Retrieve a database connection attribute //获得属性
- PDO::getAvailableDrivers — Return an array of available PDO drivers
- PDO::inTransaction — Checks if inside a transaction
- PDO::lastInsertId — Returns the ID of the last inserted row or sequence value
- PDO::prepare — Prepares a statement for execution and returns a statement object
- PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object
- PDO::quote — Quotes a string for use in a query.
- PDO::rollBack — Rolls back a transaction
- PDO::setAttribute — Set an attribute //设置属性
PDO预定义常量 (PS:可以使用预定义常量来获取一些服务器或者客户端的信息)
因为内容较多所以就不复制到文章当中了。地址:http://www.runoob.com/php/php-pdo-constants.html
语法:
PDO属性或者方法(PDO域定义变量);
案例:
<?php try { $pdo = new PDO("mysql:host=localhost;port=3306;dbname=admin","root","");//如果PDO出现异常那么就执行catch中的代码。否则不执行catch进而继续向下执行。 } catch (Exception $e) { echo "数据库连接失败!".$e->getmessage(); exit; } echo $pdo->getattribute(PDO::ATTR_DRIVER_NAME); /* *getAttribute是获取属性的信息,Attribute英译为属性。 *PDO::ATTR_DRIVER_NAME是返回驱动的名称 * */ ?>
以上是是返回信息,那么自然也是可以设置的。
<?php try { $pdo = new pdo("mysql:host=localhost;port=3306;dbname=test","root",""); } catch (PDOException $e) { echo $e->getmessage(); } echo $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,false); //该处就是设置。 ?>
本来PDO::ATTR_AUTOCOMMIT是为开启的,即为1.大家可以自己尝试执行一下。