当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 数据库 常见问题小结第1/3页

PHP 数据库 常见问题小结第1/3页

2019年05月04日  | 移动技术网IT编程  | 我要评论

钱学森的女儿,沪剧杨飞飞,火影忍者下载mp4

如果只有一种 方式使用数据库是正确的……

您可以用很多的方式创建数据库设计、数据库访问和基于数据库的 php 业务逻辑代码,但最终一般以错误告终。本文说明了数据库设计和访问数据库的 php 代码中出现的五个常见问题,以及在遇到这些问题时如何修复它们。

问题 1:直接使用 mysql

一个常见问题是较老的 php 代码直接使用 mysql_ 函数来访问数据库。清单 1 展示了如何直接访问数据库。

以下为引用的内容:

<?php
function get_user_id( $name )
{
$db = mysql_connect( 'localhost', 'root', 'password' );
mysql_select_db( 'users' );

$res = mysql_query( "select id from users where login='".$name."'" );
while( $row = mysql_fetch_array( $res ) ) { $id = $row[0]; }

return $id;
}

var_dump( get_user_id( 'jack' ) );
?>

清单 1. access/get.php
注意使用了 mysql_connect 函数来访问数据库。还要注意查询,其中使用字符串连接来向查询添加 $name 参数。

该技术有两个很好的替代方案:pear db 模块和 php data objects (pdo) 类。两者都从特定数据库选择提供抽象。因此,您的代码无需太多调整就可以在 ibm® db2®、mysql、postgresql 或者您想要连接到的任何其他数据库上运行。

使用 pear db 模块和 pdo 抽象层的另一个价值在于您可以在 sql 语句中使用 ? 操作符。这样做可使 sql 更加易于维护,且可使您的应用程序免受 sql 注入攻击。

使用 pear db 的替代代码如下所示。

以下为引用的内容:

<?php
require_once("db.php");

function get_user_id( $name )
{
$dsn = 'mysql://root:password@localhost/users';
$db =& db::connect( $dsn, array() );
if (pear::iserror($db)) { die($db->getmessage()); }

$res = $db->query( 'select id from users where login=?',
array( $name ) );
$id = null;
while( $res->fetchinto( $row ) ) { $id = $row[0]; }

return $id;
}

var_dump( get_user_id( 'jack' ) );
?>

清单 2. access/get_good.php
注意,所有直接用到 mysql 的地方都消除了,只有 $dsn 中的数据库连接字符串除外。此外,我们通过 ? 操作符在 sql 中使用 $name 变量。然后,查询的数据通过 query() 方法末尾的 array 被发送进来。

问题 2:不使用自动增量功能

与大多数现代数据库一样,mysql 能够在每记录的基础上创建自动增量惟一标识符。除此之外,我们仍然会看到这样的代码,即首先运行一个 select 语句来找到最大的 id,然后将该 id 增 1,并找到一个新记录。清单 3 展示了一个示例坏模式。

复制代码 代码如下:

drop table if exists users;
create table users (
id mediumint,
login text,
password text
);
insert into users values ( 1, 'jack', 'pass' );
insert into users values ( 2, 'joan', 'pass' );
insert into users values ( 1, 'jane', 'pass' );

清单 3. badid.sql

这里的 id 字段被简单地指定为整数。所以,尽管它应该是惟一的,我们还是可以添加任何值,如 create 语句后面的几个 insert 语句中所示。清单 4 展示了将用户添加到这种类型的模式的 php 代码。
复制代码 代码如下:

<?php
require_once("db.php");

function add_user( $name, $pass )
{
$rows = array();

$dsn = 'mysql://root:password@localhost/bad_badid';
$db =& db::connect( $dsn, array() );
if (pear::iserror($db)) { die($db->getmessage()); }

$res = $db->query( "select max(id) from users" );
$id = null;
while( $res->fetchinto( $row ) ) { $id = $row[0]; }

$id += 1;

$sth = $db->prepare( "insert into users values(?,?,?)" );
$db->execute( $sth, array( $id, $name, $pass ) );

return $id;
}

$id = add_user( 'jerry', 'pass' );

var_dump( $id );
?>

清单 4. add_user.php

add_user.php 中的代码首先执行一个查询以找到 id 的最大值。然后文件以 id 值加 1 运行一个 insert 语句。该代码在负载很重的服务器上会在竞态条件中失败。另外,它也效率低下。

那么替代方案是什么呢?使用 mysql 中的自动增量特性来自动地为每个插入创建惟一的 id。更新后的模式如下所示。
复制代码 代码如下:

drop table if exists users;
create table users (
id mediumint not null auto_increment,
login text not null,
password text not null,
primary key( id )
);

insert into users values ( null, 'jack', 'pass' );
insert into users values ( null, 'joan', 'pass' );
insert into users values ( null, 'jane', 'pass' );

清单 5. goodid.php

我们添加了 not null 标志来指示字段必须不能为空。我们还添加了 auto_increment 标志来指示字段是自动增量的,添加 primary key 标志来指示那个字段是一个 id。这些更改加快了速度。清单 6 展示了更新后的 php 代码,即将用户插入表中。
复制代码 代码如下:

<?php
require_once("db.php");

function add_user( $name, $pass )
{
$dsn = 'mysql://root:password@localhost/good_genid';
$db =& db::connect( $dsn, array() );
if (pear::iserror($db)) { die($db->getmessage()); }

$sth = $db->prepare( "insert into users values(null,?,?)" );
$db->execute( $sth, array( $name, $pass ) );

$res = $db->query( "select last_insert_id()" );
$id = null;
while( $res->fetchinto( $row ) ) { $id = $row[0]; }

return $id;
}

$id = add_user( 'jerry', 'pass' );

var_dump( $id );
?>

清单 6. add_user_good.php
1

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网