php 自己写的简单数据库类(pdo)
来源:互联网 发布:linux下如何同步时钟 编辑:程序博客网 时间:2024/06/08 19:39
<span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;"></span><h1 style="margin: 0px 10px 10px 0px; padding: 0px; font-size: 28px; color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; background-color: rgb(242, 242, 242);">如何使用PDO连接数据库,操作数据库</h1><?php<br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$dbms=’mysql’; //数据库类型 Oracle 用ODI,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数了</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$host=’localhost’; //数据库主机名</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$dbName=’test’; //使用的数据库</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$user=’root’; //数据库连接用户名</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$pass=”; //对应的密码</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$dsn=”$dbms:host=$host;dbname=$dbName”;</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">//</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">try {</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$dbh</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">echo ”连接成功”;</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">/*你还可以进行一次搜索操作</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">foreach ($dbh->query(‘SELECT * from FOO’) as $row) {</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">print_r($row); //你可以用 echo($GLOBAL); 来看到这些值</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">}</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">*/</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$dbh = null;</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">} catch (PDOException $e) {</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">die (“Error!: ” . $e->getMessage() . ””);</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">}</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">?></span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">★数据库查询:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">上面我们已经进行了一次查询,我们还可以使用如下的查询:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;"><?php</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); //设置属性</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$rs = $db->query(“SELECT * FROM foo”);</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$rs->setFetchMode(PDO::FETCH_ASSOC);</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$result_arr = $rs->fetchAll();</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">print_r($result_arr);</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">?></span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">以上因为用到setAttribute()方法,放上那两个参数,把字段名强制转换成大写。下面列出多有PDO::setAttribute()的参数:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_CASE: 强制列名变成一种格式,详细如下(第二个参数):</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::CASE_LOWER: 强制列名是小写.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::CASE_NATURAL: 列名按照原始的方式</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::CASE_UPPER: 强制列名为大写.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_ERRMODE: 错误提示.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::ERRMODE_SILENT: 不显示错误信息,只显示错误码.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::ERRMODE_WARNING: 显示警告错误.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::ERRMODE_EXCEPTION: 抛出异常.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_ORACLE_NULLS (不仅仅是ORACLE有效,别的数据库也有效): )指定数据库返回的NULL值在php中对应的数值。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::NULL_NATURAL: 不变.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::NULL_EMPTY_STRING: Empty string is converted to NULL.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">o PDO::NULL_TO_STRING: NULL is converted to an empty string.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::ATTR_AUTOCOMMIT (available in OCI, Firebird and MySQL): Whether to autocommit every single statement.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (available in MySQL): Use buffered queries.</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">例子中的$rs->setFetchMode(PDO::FETCH_ASSOC);是PDOStatement::setFetchMode(),对返回类型的声明。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">有如下:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::FETCH_ASSOC – 关联数组形式</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::FETCH_NUM – 数字索引数组形式</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::FETCH_BOTH – 两者数组形式都有,这是缺省的</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::FETCH_OBJ – 按照对象的形式,类似于以前的 mysql_fetch_object()</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">更多返回类型声明(PDOStatement::方法名)看手册。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">★插入,更新,删除数据,</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">$db->exec(“DELETE FROM `xxxx_menu` where mid=43″);</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• 简单的总结一下上面的操作:</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">查询操作主要是PDO::query()、PDO::exec()、PDO::prepare()。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::query()主要是用于有记录结果返回的操作,特别是SELECT操作,</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::exec()主要是针对没有结果集合返回的操作,比如INSERT、UPDATE、DELETE等操作,它返回的结果是当前操作影响的列数。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDO::prepare()主要是预处理操作,需要通过$rs->execute()来执行预处理里面的SQL语句,这个方法可以绑定参数,功能比较强大,不是本文能够简单说明白的,大家可以参考手册和其他文档。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">获取结果集操作主要是:PDOStatement::fetchColumn()、PDOStatement::fetch()、PDOStatement::fetchALL()。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDOStatement::fetchColumn() 是获取结果指定第一条记录的某个字段,缺省是第一个字段。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDOStatement::fetch() 是用来获取一条记录,</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDOStatement::fetchAll()是获取所有记录集到一个中,获取结果可以通过PDOStatement::setFetchMode来设置需要结果集合的类型。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">另外有两个周边的操作,一个是PDO::lastInsertId()和PDOStatement::rowCount()。PDO::lastInsertId()是返回上次插入操作,主键列类型是自增的最后的自增ID。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">PDOStatement::rowCount()主要是用于PDO::query()和PDO::prepare()进行DELETE、INSERT、UPDATE操作影响的结果集,对PDO::exec()方法和SELECT操作无效。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">★事务和自动提交</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">至此,您已经通过 PDO 连接到了 mysql,在发出查询之前,您应该理解 PDO 是如何管理事务的。如果之前没有接触过事务,那么首先要知道事务的 4 个特征:原子性(Atomicity)、一致性(Consistency)、独立性(Isolation)和持久性(Durability),即 ACID。用外行人的话说,对于在一个事务中执行的任何工作,即使它是分阶段执行的,也一定可以保证该工作会安全地应用于数据库,并且在工作被提交时,不会受到来自其他连接的影响。事务性工作可以根据请求自动撤销(假设您还没有提交它),这使得脚本中的错误处理变得更加容易。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">事务通常是通过把一批更改积蓄起来、使之同时生效而实现的。这样做的好处是可以大大提高这些更新的效率。换句话说,事务可以使脚本更快,而且可能更健壮(不过需要正确地使用事务才能获得这样的好处)。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">0位粉丝 5楼</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">• 提供给预处理语句的参数不需要用引号括起来,驱动程序会处理这些。如果应用程序独占地使用预处理语句,那么可以确保没有 SQL 入侵发生。(然而,如果您仍然将查询的其他部分建立在不受信任的输入之上,那么就仍然存在风险)。</span><br style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;" /><span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;">预处理语句是如此有用,以致 PDO 实际上打破了在目标 4 中设下的规则:如果驱动程序不支持预处理语句,那么 PDO 将仿真预处理语句。</span>
<span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;"></span>
<span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;"></span>
<span style="color: rgb(51, 51, 51); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 25px;"></span>
<?php class PDO_db{ private $type; private $host; private $port; private $user; private $pass; private $char; private $db; private $prodb; public $stmt; public function __construct($arr = array()){ $this ->type = isset($arr['type']) ? $arr['type'] : 'mysql'; $this ->host = isset($arr['host']) ? $arr['host'] : '127.0.0.1'; $this ->port = isset($arr['port']) ? $arr['port'] : '3306'; $this ->user = isset($arr['user']) ? $arr['user'] : 'root'; $this ->pass = isset($arr['pass']) ? $arr['pass'] : 'root'; $this ->char = isset($arr['char']) ? $arr['char'] : 'utf8'; $this ->db = isset($arr['db']) ? $arr['db'] : 'school'; $this ->conn(); $this ->dbexec("use {$this->db}"); $this ->dbexec("set names {$this ->char}"); } private function conn(){ $this ->prodb=new PDO("{$this ->type}:host={$this ->host};port={$this ->port};dbname={$this ->db}",$this ->user,$this ->pass); if(!$this ->prodb){die('数据库连接失败!');}; $this->prodb->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } private function geterr($e){ echo 'SQL 请求出错<br/>'."错误:{$e->errorInfo[2]}<br/>line{$e->getLine()}"; exit; } private function dbexec($sql){ try{ return $this->prodb->exec($sql); }catch(PDOException $e){ $this->geterr($e); }; } public function ins_del_db($sql){ $affect=$this->prodb->exec($sql); $id=$this->prodb->lastinsertid(); return $id != 0?$id:$affect; } public function selectdb($sql){ try{ $this->stmt= $this->prodb->query($sql); $arr=array(); while($res=$this->stmt->fetch(PDO::FETCH_ASSOC)){ $arr[]=$res; }; return $arr; }catch(PDOException $e){ $this->geterr($e); }; }}header("content-type:text/html;charset=utf-8");$mydb=new PDO_db();//var_dump($mydb->ins_del_db("insert into pr_iuser values (null,'xiaoshan',{SHA1(789456)})"));echo '<pre>';var_dump($mydb->selectdb("select * from pr_student"));?>
0 0
- php 自己写的简单数据库类(pdo)
- php 自己写的简单数据库类
- 自己写的数据库pdo操作类,显摆一下
- PHP PDO数据库类
- 简单的pgsql pdo php操作类
- php的PDO连接数据库(一)
- php的PDO连接数据库(二)
- 简单的PDO数据库操作类
- php数据库抽象层PDO的简单测试
- php pdo 简单连接数据库、查询
- PHP:再写pdo dsn的时候 选择数据库的时候不要写空格
- 自己写的简单的数据库访问类
- PHP的PDO类
- php-简单后台图书管理系统-数据库表的创建以及pdo方式数据库的封装工具类01
- 自己写好的pdo数据库抽象层 mysql为例
- php的mysql函数仿pdo操作数据库类
- 同时支持三个MySQL+SQLite+PDO的PHP数据库类
- php PDO事务的简单应用
- 非递归遍历
- php 自己写的简单数据库类
- win7/ping优化
- poj 2454 随机化(划片使得选举胜利)
- 【学习笔记】tableview 文字设置 缩进,头尾标题 间距,cell高度 点击 分割线
- php 自己写的简单数据库类(pdo)
- asp.net mvc 部署
- Windows下安装Python数据库模块--MySQLdb
- 搭建vsftpd(本地用户和虚拟用户可以同时存在)
- poj 2689 Prime Distance
- 理解OAuth 2.0
- iOS操作系统整体架构层次
- hdu 5198 简单题
- Java中的MD5算法