PHP和MySQL开发的8个技巧



LAMP 架构网站WebSite我以前注重多是安装/配置方面讲述开发相对较少自己从事开发也少本文原文当然也来自:

Published _disibledevent=>$temp[1] = \"tigers\";
$temp[2] = \"premiers\";

for($x=0;$x<count($temp);$x)
{
echo $temp[$x];
echo \" \";
}
?>

然而另外种更加节省代码方式是:

<?php
$temp = .gif' />(\"richmond\", \"tigers\", \"premiers\");
foreach ($temp as $element)
echo \"$element \";
?>

foreach 还能输出文字下标:

<?php
$temp = .gif' />(\"club\" => \"richmond\",
\"nickname\" =>\"tigers\",
\"aim\" => \"premiers\");

foreach ($temp as $key => $value)
echo \"$key : $value \";
?>
PHP 手册中描隽舜笤?50 个用于处理

2. 在 PHP 串中加入变量

这个很简单:

<?php
$temp = \"hello\"
echo \"$temp world\";
?>

但是需要介绍说明尽管下面例子没有:
<?php
$temp = .gif' />(\"one\" => 1, \"two\" => 2);
// 输出:: The first element is 1
echo \"The first element is $temp[one].\";
?>

但是如果后面那个 echo 语句没有双引号引起来就要报错因此建议使用花括号:

<?php
$temp = .gif' />(\"one\" => 1, \"two\" => 2);
echo \"The first element is {$temp[\"one\"]}.\";
?>


3. 采用关联存取查询结果
看下面例子:

<?php
$connection = mysql_connect(\"localhost\", \"albert\", \"shhh\");
mysql_select_db(\"winestore\", $connection);

$result = mysql_query(\"SELECT cust_id, surname,
firstname FROM customer\", $connection);

while ($row = mysql_fetch_.gif' />($result))
{
echo \"ID:\\t{$row[\"cust_id\"]}\\n\";
echo \"Surname\\t{$row[\"surname\"]}\\n\";
echo \"First name:\\t{$row[\"firstname\"]}\\n\\n\";
}
?>

mysql_fetch_.gif' /> 把查询结果行放入可以同时用两种方式引用例如 cust_id 可以同时用下面两种方式:$row[\"cust_id\"] 或者$row[0] 显然前者可读性要比后者好多了

在多表连查中如果两个列名字最好用别名分开:

SELECT winery.name AS wname,
region.name AS rname,
FROM winery, region
WHERE winery.region_id = region.region_id;


列名引用为:$row[\"wname\"] 和 $row[\"rname\"]


在指定表名和列名情况下只引用列名:

SELECT winery.region_id
FROM winery

列名引用为: $row[\"region_id\"]

聚集引用就是引用名:

SELECT count(*)
FROM customer;

列名引用为: $row[\"count(*)\"]

4. 注意常见 PHP bug

常见 PHP 纠错问题是:

No page rendered by the Web browser when much more is expected
A pop-up dialog stating that the \"Document Contains No Data\"
A partial page when more is expected

出现这些情况大多数原因并不在于脚本逻辑而是 HTML 中存在 bug 或者脚本生成 HTML bug 例如缺少类似 </table> </form> </frame> 的类关闭 Tag页面就不能刷新解决这个问题办法就是查看 HTML 源代码

对于复杂不能查到原因页面可以通过 W3C 页面校验 http://validator.w3.org/ 来分析

如果没有定义变量或者变量定义也会让变得古怪例如下面死循环:

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

变量 $Counter 在增加而 $counter 永远小于 10这类般都能通过设置较高报告级别来找到:

<?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.php?surname=Smith&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);
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 执行完查询后马上返回不需要等待或者对数据库加锁
Tags: 

延伸阅读

最新评论

发表评论