我们在进行清单 1. Access/get.php
<ol class="dp-xml"> <li class="alt"><span><span><?php </span></span></li> <li class=""><span>function get_user_id( $name ) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_connect</font></span><span>( 'localhost', 'root', 'password' ); </span> </li> <li class="alt"><span> mysql_select_db( 'users' ); </span></li> <li class=""><span> </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">res</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>( "SELECT id FROM users WHERE </span><span class="attribute"><font color="#ff0000">login</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'".$name."'</font></span><span>" ); </span> </li> <li class=""> <span> while( $</span><span class="attribute"><font color="#ff0000">row</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_fetch_array</font></span><span>( $res ) ) { $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = $row[0]; } </span> </li> <li class="alt"><span> </span></li> <li class=""><span> return $id; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>var_dump( get_user_id( 'jack' ) ); </span></li> <li class=""><span>?> </span></li> </ol>
注意使用了 mysql_connect 函数来实现PHP直接使用MySQL。还要注意查询,其中使用字符串连接来向查询添加 $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-xml"> <li class="alt"><span><span><?php </span></span></li> <li class=""><span>require_once("DB.php"); </span></li> <li class="alt"><span> </span></li> <li class=""><span>function get_user_id( $name ) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">dsn</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'mysql://root:password@localhost/users'</font></span><span>; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">db</font></span><span> =& DB::Connect( $dsn, array() ); </span> </li> <li class=""><span> if (PEAR::isError($db)) { die($db->getMessage()); } </span></li> <li class="alt"><span> </span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">res</font></span><span> = $db->query( 'SELECT id FROM users WHERE </span><span class="attribute"><font color="#ff0000">login</font></span><span>=?',array( $name ) ); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>; </span> </li> <li class=""> <span> while( $res->fetchInto( $row ) ) { $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = $row[0]; } </span> </li> <li class="alt"><span> </span></li> <li class=""><span> return $id; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><span>var_dump( get_user_id( 'jack' ) ); </span></li> <li class=""><span>?> </span></li> </ol>
注意,所有PHP直接使用MySQL的地方都消除了,只有 $dsn 中的数据库连接字符串除外。此外,我们通过 ? 操作符在 SQL 中使用 $name 变量。然后,查询的数据通过 query() 方法末尾的 array 被发送进来。