linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
PHP和MySQL开发的8个技巧
[ 作者:  加入时间:2008-01-28 13:55:56  来自:Linux联盟收集整理 ]
 LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。本文的原文当然也来自: Xh8Linux联盟
   Xh8Linux联盟
  Published on The O'Reilly Network (http://www.oreillynet.com/) Xh8Linux联盟
  http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html Xh8Linux联盟
   Xh8Linux联盟
  看了以后,颇有启发,以前开发中遇到的一些问题,迎刃而解。所以翻译出来和大家共享。 Xh8Linux联盟
   Xh8Linux联盟
  1. PHP 中数组的使用 Xh8Linux联盟
  在操作数据库时,使用关联数组(associatively-indexed arrays)十分有帮助,下面我们看一个基本的数字格式的数组遍历: Xh8Linux联盟
   Xh8Linux联盟
  $temp[0] = "richmond"; Xh8Linux联盟
  $temp[1] = "tigers"; Xh8Linux联盟
  $temp[2] = "premiers"; Xh8Linux联盟
   Xh8Linux联盟
  for($x=0;$x Xh8Linux联盟
  { Xh8Linux联盟
  echo $temp[$x]; Xh8Linux联盟
  echo " "; Xh8Linux联盟
  } Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  然而另外一种更加节省代码的方式是: Xh8Linux联盟
   Xh8Linux联盟
  $temp = array("richmond", "tigers", "premiers"); Xh8Linux联盟
  foreach ($temp as $element) Xh8Linux联盟
  echo "$element "; Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  foreach 还能输出文字下标: Xh8Linux联盟
   Xh8Linux联盟
  $temp = array("club" => "richmond", Xh8Linux联盟
  "nickname" =>"tigers", Xh8Linux联盟
  "aim" => "premiers"); Xh8Linux联盟
   Xh8Linux联盟
  foreach ($temp as $key => $value) Xh8Linux联盟
  echo "$key : $value "; Xh8Linux联盟
  ?> Xh8Linux联盟
  PHP 手册中描述了大约 50 个用于处理数组的函数。 Xh8Linux联盟
   Xh8Linux联盟
  2. 在 PHP 字符串中加入变量 Xh8Linux联盟
   Xh8Linux联盟
  这个很简单的: Xh8Linux联盟
  $temp = "hello" Xh8Linux联盟
  echo "$temp world"; Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  但是需要说明的是,尽管下面的例子没有错误: Xh8Linux联盟
  $temp = array("one" => 1, "two" => 2); Xh8Linux联盟
  // 输出:: The first element is 1 Xh8Linux联盟
  echo "The first element is $temp[one]."; Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号: Xh8Linux联盟
   Xh8Linux联盟
  $temp = array("one" => 1, "two" => 2); Xh8Linux联盟
  echo "The first element is {$temp["one"]}."; Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
   Xh8Linux联盟
  3. 采用关联数组存取查询结果 Xh8Linux联盟
  看下面的例子: Xh8Linux联盟
   Xh8Linux联盟
  $connection = MySQL_connect("localhost", "albert", "shhh"); Xh8Linux联盟
  mysql_select_db("winestore", $connection); Xh8Linux联盟
   Xh8Linux联盟
  $result = mysql_query("SELECT cust_id, surname, Xh8Linux联盟
  firstname FROM customer", $connection); Xh8Linux联盟
   Xh8Linux联盟
  while ($row = mysql_fetch_array($result)) Xh8Linux联盟
  { Xh8Linux联盟
  echo "ID:\t{$row["cust_id"]}\n"; Xh8Linux联盟
  echo "Surname\t{$row["surname"]}\n"; Xh8Linux联盟
  echo "First name:\t{$row["firstname"]}\n\n"; Xh8Linux联盟
  } Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  函数 mysql_fetch_array() 把查询结果的一行放入数组,可以同时用两种方式引用,例如 cust_id 可以同时用下面两种方式:$row["cust_id"] 或者$row[0] 。显然,前者的可读性要比后者好多了。 Xh8Linux联盟
   Xh8Linux联盟
  在多表连查中,如果两个列名字一样,最好用别名分开: Xh8Linux联盟
   Xh8Linux联盟
  SELECT winery.name AS wname, Xh8Linux联盟
  region.name AS rname, Xh8Linux联盟
  FROM winery, region Xh8Linux联盟
  WHERE winery.region_id = region.region_id; Xh8Linux联盟
   Xh8Linux联盟
列名的引用为:$row["wname"] 和 $row["rname"]。 Xh8Linux联盟
   Xh8Linux联盟
  在指定表名和列名的情况下,只引用列名: Xh8Linux联盟
   Xh8Linux联盟
  SELECT winery.region_id Xh8Linux联盟
  FROM winery Xh8Linux联盟
   Xh8Linux联盟
  列名的引用为: $row["region_id"]。 Xh8Linux联盟
   Xh8Linux联盟
  聚集函数的引用就是引用名: Xh8Linux联盟
   Xh8Linux联盟
  SELECT count(*) Xh8Linux联盟
  FROM customer; Xh8Linux联盟
   Xh8Linux联盟
  列名的引用为: $row["count(*)"]。 Xh8Linux联盟
   Xh8Linux联盟
  4. 注意常见的 PHP bug Xh8Linux联盟
  常见的 PHP 纠错问题是: Xh8Linux联盟
   Xh8Linux联盟
  No page rendered by the Web browser when much more is expected Xh8Linux联盟
  A pop-up dialog stating that the "Document Contains No Data" Xh8Linux联盟
  A partial page when more is expected Xh8Linux联盟
   Xh8Linux联盟
  出现这些情况的大多数原因并不在于脚本的逻辑,而是 HTML 中存在的 bug 或者脚本生成的 HTML 的 bug 。例如缺少类似,之类的关闭 Tag,页面就不能刷新。解决这个问题的办法就是,查看 HTML 的源代码。 Xh8Linux联盟
   Xh8Linux联盟
  对于复杂的,不能查到原因的页面,可以通过 W3C 的页面校验程序 http://validator.w3.org/ 来分析。 Xh8Linux联盟
   Xh8Linux联盟
  如果没有定义变量,或者变量定义错误也会让程序变得古怪。例如下面的死循环: Xh8Linux联盟
   Xh8Linux联盟
  for($counter=0; $counter<10; $Counter++) Xh8Linux联盟
  myFunction(); Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  变量 $Counter 在增加,而 $counter 永远小于 10。这类错误一般都能通过设置较高的错误报告级别来找到: Xh8Linux联盟
   Xh8Linux联盟
  error_reporting(E_ALL); Xh8Linux联盟
   Xh8Linux联盟
  for($counter=0; $counter<10; $Counter++) Xh8Linux联盟
  myFunction(); Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  5. 采用 header() 函数处理单部件查询 Xh8Linux联盟
   Xh8Linux联盟
  在很多 Web 数据库应用中,一些功能往往让用户点击一个连接后,继续停留在当前页面,这样的工作我叫它“单部件查询”。 Xh8Linux联盟
   Xh8Linux联盟
  下面是一个叫做 calling.php 的脚本: Xh8Linux联盟
   Xh8Linux联盟
  "-//W3C//DTD HTML 4.0 Transitional//EN" Xh8Linux联盟
  "http://www.w3.org/TR/html4/loose.dtd" > Xh8Linux联盟
   当用户点击上面的连接时,就去调用 action.PHP。下面是 action.php 的源码: Xh8Linux联盟
   Xh8Linux联盟
  // 数据库功能 Xh8Linux联盟
   Xh8Linux联盟
  // 重定向 Xh8Linux联盟
  header("Location: $HTTP_REFERER"); Xh8Linux联盟
  exit; Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  这里有两个常见的错误需要提醒一下: Xh8Linux联盟
  调用 header() 函数后要包含一个 exit 语句让脚本停止,否则后续的脚本可能会在头发送前输出。 Xh8Linux联盟
   Xh8Linux联盟
  header() 函数常见的一个错误是: Xh8Linux联盟
   Xh8Linux联盟
  Warning: Cannot add header information - headers already sent... Xh8Linux联盟
   Xh8Linux联盟
  header() 函数只能在 HTML 输出之前被调用,因此你需要检查 php 前面可能存在的空行,空格等等。 Xh8Linux联盟
   Xh8Linux联盟
  6. reload 的问题及其解决 Xh8Linux联盟
  我以前在写 PHP 程序时,经常碰到页面刷新时,数据库多处理一次的情况。 Xh8Linux联盟
  我们来看 addcust.php: Xh8Linux联盟
   Xh8Linux联盟
  $query = "INSERT INTO customer Xh8Linux联盟
  SET surname = $surname, Xh8Linux联盟
  firstname = $firstname"; Xh8Linux联盟
  $connection = MySQL_connect("localhost", "fred", "shhh"); Xh8Linux联盟
  mysql_select_db("winestore", $connection); Xh8Linux联盟
  $result = mysql_query($query, $connection); Xh8Linux联盟
  ?> Xh8Linux联盟
   Xh8Linux联盟
  "-//W3C//DTD HTML 4.0 Transitional//EN" Xh8Linux联盟
  "http://www.w3.org/TR/html4/loose.dtd" > Xh8Linux联盟
   Xh8Linux联盟
  I've inserted the customer for you. Xh8Linux联盟
   Xh8Linux联盟
  ?> Xh8Linux联盟
  假设我们用下面的连接使用这个程序: Xh8Linux联盟
   Xh8Linux联盟
  http://www.freelamp.com/addcust.php?surname=Smith&firstname=Fred Xh8Linux联盟
   Xh8Linux联盟
  如果这个请求只提交一次,OK ,不会有问题,但是如果多次刷新,你就会有多条记录插入。 Xh8Linux联盟
  这个问题可以通过 header() 函数解决:下面是新版本的 addcust.php: Xh8Linux联盟
   Xh8Linux联盟
  $query = "INSERT INTO customer Xh8Linux联盟
  SET surname = $surname, Xh8Linux联盟
  firstname = $firstname"; Xh8Linux联盟
  $connection = mysql_connect("localhost", "fred", "shhh"); Xh8Linux联盟
  mysql_select_db("winestore", $connection); Xh8Linux联盟
  $result = mysql_query($query, $connection); Xh8Linux联盟
  header("Location: cust_receipt.php"); Xh8Linux联盟
  ?> Xh8Linux联盟
  这个脚本把浏览器重定向到一个新的页面:cust_receipt.php: Xh8Linux联盟
   Xh8Linux联盟
  "-//W3C//DTD HTML 4.0 Transitional//EN" Xh8Linux联盟
  "http://www.w3.org/TR/html4/loose.dtd" > Xh8Linux联盟
   Xh8Linux联盟
  I've inserted the customer for you. Xh8Linux联盟
   Xh8Linux联盟
   Xh8Linux联盟
  这样,原来的页面继续刷新也没有副作用了。 Xh8Linux联盟
   Xh8Linux联盟
  7. 巧用锁机制来提高应用性能 Xh8Linux联盟
  如果我们要紧急运行一个报表,那么,我们可以对表加写锁,防治别人读写,来提高对这个表的处理速度。 Xh8Linux联盟
   Xh8Linux联盟
  8. 用 mysql_unbuffered_query() 开发快速的脚本 Xh8Linux联盟
  这个函数能用来替换 mysql_query() 函数,主要的区别就是 mysql_unbuffered_query() 执行完查询后马上返回,不需要等待或者对数据库加锁。 Xh8Linux联盟
   Xh8Linux联盟
  但是返回的行数不能用mysql_num_rows() 函数来检查,因为输出的结果集大小未知。 Xh8Linux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息