LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。看了以后,颇有启发,以前开发中遇到的一些问题,迎刃而解。所
1. PHP 中数组的使用
在操作数据库时,香港虚拟主机,使用关联数组(associatively-indexed arrays)十分有帮助,下面我们看一个基本的数字格式的数组遍历:
复制代码 代码如下:
$temp[0] = "richmond";
$temp[1] = "tigers";
$temp[2] = "premiers";
for($x=0;$x
echo $temp[$x];
echo " ";
}
?>
然而另外一种更加节省代码的方式是:
复制代码 代码如下:
$temp = array("richmond", "tigers", "premiers");
foreach ($temp as $element)
echo "$element ";
?>
foreach 还能输出文字下标:
复制代码 代码如下:
$temp = array("club" => "richmond",
"nickname" =>"tigers",
"aim" => "premiers");
foreach ($temp as $key => $value)
echo "$key : $value ";
?>
PHP 手册中描述了大约 50 个用于处理数组的函数。
2. 在 PHP 字符串中加入变量
这个很简单的:
复制代码 代码如下:
$temp = "hello"
echo "$temp world";
?>
但是需要说明的是,尽管下面的例子没有错误:
复制代码 代码如下:
$temp = array("one" => 1, "two" => 2);
// 输出:: The first element is 1
echo "The first element is $temp[one].";
?>
但是如果后面那个 echo 语句没有双引号引起来的话,就要报错,因此建议使用花括号:
复制代码 代码如下:
$temp = array("one" => 1, "two" => 2);
echo "The first element is {$temp["one"]}.";
?>
3. 采用关联数组存取查询结果
看下面的例子:
复制代码 代码如下:
$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 。例如缺少类似 , , 之类的关闭 Tag,页面就不能刷新。解决这个问题的办法就是,查看 HTML 的源代码。
对于复杂的,不能查到原因的页面,可以通过 W3C 的页面校验程序 来分析。
如果没有定义变量,或者变量定义错误也会让程序变得古怪。例如下面的死循环:
复制代码 代码如下:
for($counter=0; $countermyFunction();
?>
变量 $Counter 在增加,而 $counter 永远小于 10。这类错误一般都能通过设置较高的错误报告级别来找到:
复制代码 代码如下:
error_reporting(E_ALL);
for($counter=0; $countermyFunction();
?>
5. 采用 header() 函数处理单部件查询
在很多 Web 数据库应用中,一些功能往往让用户点击一个连接后,继续停留在当前页面,这样的工作我叫它“单部件查询”。
下面是一个叫做 calling.php 的脚本:
复制代码 代码如下:
Click here!
当用户点击上面的连接时,就去调用 action.php。下面是 action.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:
复制代码 代码如下:
$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);
?>
BR>"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
I've inserted the customer for you.
?>
假设我们用下面的连接使用这个程序:
... &firstname=Fred
如果这个请求只提交一次,OK ,香港服务器租用,不会有问题,服务器空间,但是如果多次刷新,你就会有多条记录插入。
这个问题可以通过 header() 函数解决:下面是新版本的 addcust.php:
复制代码 代码如下:

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
