如果只有一种方式使用数据库是正确的,您可以用很多的方式创建数据库设计、数据库访问和基于数据库的 PHP 业务逻辑代码,但最终一般以错误告终。本文说明了数据库设计和访问数据库的 PHP 代码中出现的五个常见问题,以及在遇到这些问题时如何修复它们。
问题 1:直接使用 MySQL
一个常见问题是较老的 PHP 代码直接使用 mysql_ 函数来访问数据库。清单 1 展示了如何直接访问数据库。
清单 1. Access/get.php
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li> <span class="keyword">function</span><span> get_user_id( </span><span class="vars">$name</span><span> ) </span> </li> <li class="alt"><span>{ </span></li> <li> <span> </span><span class="vars">$db</span><span> = mysql_connect( </span><span class="string">'localhost'</span><span>, </span><span class="string">'root'</span><span>, </span><span class="string">'password'</span><span> ); </span> </li> <li class="alt"> <span> mysql_select_db( </span><span class="string">'users'</span><span> ); </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="vars">$res</span><span> = mysql_query( </span><span class="string">"SELECT id FROM users WHERE login='"</span><span>.</span><span class="vars">$name</span><span>.</span><span class="string">"'"</span><span> ); </span> </li> <li> <span> </span><span class="keyword">while</span><span>( </span><span class="vars">$row</span><span> = mysql_fetch_array( </span><span class="vars">$res</span><span> ) ) { </span><span class="vars">$id</span><span> = </span><span class="vars">$row</span><span>[0]; } </span> </li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$id</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span>var_dump( get_user_id( </span><span class="string">'jack'</span><span> ) ); </span> </li> <li><span>?> </span></li> </ol>
注意使用了 mysql_connect 函数来访问数据库。还要注意查询,其中使用字符串连接来向查询添加 $name 参数。
该技术有两个很好的替代方案:PEAR DB 模块和 PHP Data Objects (PDO) 类。两者都从特定数据库选择提供抽象。因此,您的代码无需太多调整就可以在 IBM® DB2®、MySQL、PostgreSQL 或者您想要连接到的任何其他数据库上运行。
使用 PEAR DB 模块和 PDO 抽象层的另一个价值在于您可以在 SQL 语句中使用 ? 操作符。这样做可使 SQL 更加易于维护,且可使您的应用程序免受 SQL 注入攻击。
使用 PEAR DB 的替代代码如下所示。
清单 2. Access/get_good.php
<ol class="dp-c"> <span><span> </span></span><li><span><?php </span></span></li> <li class="alt"> <span class="keyword">require_once</span><span>(</span><span class="string">"DB.php"</span><span>); </span> </li> <li><span> </span></li> <li class="alt"> <span class="keyword">function</span><span> get_user_id( </span><span class="vars">$name</span><span> ) </span> </li> <li><span>{ </span></li> <li class="alt"> <span> </span><span class="vars">$dsn</span><span> = </span><span class="string">'mysql://root:password@localhost/users'</span><span>; </span> </li> <li> <span> </span><span class="vars">$db</span><span> =& DB::Connect( </span><span class="vars">$dsn</span><span>, </span><span class="keyword">array</span><span>() ); </span> </li> <li class="alt"> <span> </span><span class="keyword">if</span><span> (PEAR::isError(</span><span class="vars">$db</span><span>)) { </span><span class="keyword">die</span><span>(</span><span class="vars">$db</span><span>->getMessage()); } </span> </li> <li><span> </span></li> <li class="alt"> <span> </span><span class="vars">$res</span><span> = </span><span class="vars">$db</span><span>->query( </span><span class="string">'SELECT id FROM users WHERE login=?'</span><span>, </span> </li> <li> <span> </span><span class="keyword">array</span><span>( </span><span class="vars">$name</span><span> ) ); </span> </li> <li class="alt"> <span> </span><span class="vars">$id</span><span> = null; </span> </li> <li> <span> </span><span class="keyword">while</span><span>( </span><span class="vars">$res</span><span>->fetchInto( </span><span class="vars">$row</span><span> ) ) { </span><span class="vars">$id</span><span> = </span><span class="vars">$row</span><span>[0]; } </span> </li> <li class="alt"><span> </span></li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$id</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li><span> </span></li> <li class="alt"> <span>var_dump( get_user_id( </span><span class="string">'jack'</span><span> ) ); </span> </li> <li><span>?> </span></li> </ol>
注意,所有直接用到 MySQL 的地方都消除了,只有 $dsn 中的数据库连接字符串除外。此外,我们通过 ? 操作符在 SQL 中使用 $name 变量。然后,查询的数据通过 query() 方法末尾的 array 被发送进来。
1