当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP与MySQL开发的8个技巧小结

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

2019年04月24日  | 移动技术网IT编程  | 我要评论
1. php 中数组的使用
在操作数据库时,使用关联数组(associatively-indexed arrays)十分有帮助,下面我们看一个基本的数字格式的数组遍历:
复制代码 代码如下:

<?php
$temp[0] = "richmond";
$temp[1] = "tigers";
$temp[2] = "premiers";

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

然而另外一种更加节省代码的方式是:
复制代码 代码如下:

<?php
$temp = array("richmond", "tigers", "premiers");
foreach ($temp as $element)
echo "$element ";
?>

foreach 还能输出文字下标:
复制代码 代码如下:

<?php
$temp = array("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 = array("one" => 1, "two" => 2);
// 输出:: the first element is 1
echo "the first element is $temp[one].";
?>

但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号:
复制代码 代码如下:

<?php
$temp = array("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_array($result))
{
echo "id:\t{$row["cust_id"]}\n";
echo "surname\t{$row["surname"]}\n";
echo "first name:\t{$row["firstname"]}\n\n";
}
?>

函数 mysql_fetch_array() 把查询结果的一行放入数组,可以同时用两种方式引用,例如 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. ... &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() 执行完查询后马上返回,不需要等待或者对数据库加锁。

但是返回的行数不能用mysql_num_rows() 函数来检查,因为输出的结果集大小未知。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网