当前位置: 网学 > 编程文档 > PHP > 正文

PHP与MySQL开发的8个技巧小结

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/22
这类错误一般都能通过设置较高的错误报告级别来找到:
复制代码 代码如下:
<?php
error_reporting(E_ALL);

for($counter=0; $counter<10; $Counter++)
myFunction();
?>

5. 采用 header() 函数处理单部件查询
在很多 Web 数据库应用中,一些功能往往让用户点击一个连接后,继续停留在当前页面,这样的工作我叫它“单部件查询”。
下面是一个叫做 calling.php 的脚本:
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Calling page example</title>
</head>
<body>
<a href="action.php">Click here!</a>
</body>
</html>

当用户点击上面的连接时,就去调用 action.php。下面是 action.php 的源码:
复制代码 代码如下:
<?php
// 数据库功能
// 重定向
header("Location: $HTTP_REFERER");
exit;
?>

这里有两个常见的错误需要提醒一下:
调用 header() 函数后要包含一个 exit 语句让脚本停止,否则后续的脚本可能会在头发送前输出。

header() 函数常见的一个错误是:
Warning: Cannot add header information - headers already sent...
header() 函数只能在 HTML 输出之前被调用,因此你需要检查 php 前面可能存在的空行,空格等等。
6. reload 的问题及其解决
我以前在写 PHP 程序时,经常碰到页面刷新时,数据库多处理一次的情况。
我们来看 addcust.php:
复制代码 代码如下:
<?php
$query = "INSERT INTO customer
SET surname = $surname,
firstname = $firstname";
$connection = mysql_connect("localhost", "fred", "shhh");
mysql_select_db("winestore", $connection);
$result = mysql_query($query, $connection);
?>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Customer insert</title>
</head>
<body>
I''ve inserted the customer for you.
</body>
</html>
?>

假设我们用下面的连接使用这个程序
http://www.freelamp.com/addcust. ... &firstname=Fred
如果这个请求只提交一次,OK ,不会有问题,但是如果多次刷新,你就会有多条记录插入。
这个问题可以通过 header() 函数解决:下面是新版本的 addcust.php:
复制代码 代码如下:
<?php
$query = "INSERT INTO customer
SET surname = $surname,
firstname = $firstname";
$connection = mysql_connect("localhost", "fred", "shhh");
mysql_select_db("winestore", $connection);
$result = mysql_query($query, $connection);
he

header("Location: cust_receipt.php");
?>

这个脚本把浏览器重定向到一个新的页面:cust_receipt.php:
复制代码 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
<title>Customer insert</title>
</head>
<body>
I''ve inserted the customer for you.
</body>
</html>

这样,原来的页面继续刷新也没有副作用了。
7. 巧用锁机制来提高应用性能
如果我们要紧急运行一个报表,那么,我们可以对表加写锁,防治别人读写,来提高对这个表的处理速度。
8. 用 mysql_unbuffered_query() 开发快速的脚本
这个函数能用来替换 mysql_query() 函数,主要的区别就是 mysql_unbuffered_query() 执行完查询后马上返回,不需要等待或

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号