Home php教程 php手册 动态网页技术--CGI:ASP:JSP:PHP(4)

动态网页技术--CGI:ASP:JSP:PHP(4)

Jun 21, 2016 am 09:04 AM
echo lt mysql php

4.PHP技术
  笔者对PHP最为熟悉,也用的最多。PHP----Hypertext Preprocessor(超文本预处理器),是一种易于学习和使用的服务器端脚本语言,是生成动态网页的工具之一。它是嵌入HTML文件的一种脚本语言。七语法大部分是从C,JAVA,PERL语言中借来,并形成了自己的独有风格;目标是让WEB程序员快速的开发出动态的网页。它是当今INTERNET上最为火热的脚本语言,只需要很少的编程知识你就能使用PHP建立一个真正交互的WEB站点。
  PHP是完全免费的,可以不受限制的获得源码,甚至可以从中加进你自己需要的特色。PHP在大多数Unix平台,GUN/Linux和微软Windows平台上均可以运行。PHP的官方网站是:http://www.php.net。
  与ASP、JSP一样,PHP也可以结合HTML语言共同使用;它与HTML语言具有非常好的兼容性,使用者可以直接在脚本代码中加入HTML标签,或者在HTML标签中加入脚本代码从而更好的实现页面控制,提供更加丰富的功能。
  PHP的优点有:安装方便学习过程简单;数据库连接方便,兼容性强;扩展性强;可以进行面向对象编程。引用Nissan的Xterra的话来说就是PHP可以做到你想让它做到的一切而且无所不能!
  PHP提供了标准的数据库接口,几乎可以连接所有的数据库;尤其和MYSQL数据库的配合更是"天衣无缝"。下面引用一个调用MYSQL数据库并分页显示的例子来加深对PHP的了解。

$pagesize = 5; //每页显示5条记录
$host="localhost";
$user="user";
$password="psw";
$dbname="book"; //所查询的库表名;
//连接MySQL数据库
mysql_connect("$host","$user","$password") or die("无法连接MySQL数据库服务器!");

$db = mysql_select_db("$dbname") or die("无法连接数据库!");

$sql = "select count(*) as total from pagetest";//生成查询记录数的SQL语句
$rst = mysql_query($sql) or die("无法执行SQL语句:$sql !"); //查询记录数
$row = mysql_fetch_array($rst) or die("没有更多的记录!"); /取出一条记录
$rowcount = $row["total"];//取出记录数
mysql_free_result($rst) or die("无法释放result资源!"); //释放result资源

$pagecount = bcdiv($rowcount+$pagesize-1,$pagesize,0);//算出总共有几页

if(!isset($pageno)) {
$pageno = 1; //在没有设置pageno时,缺省为显示第1页
}
if($pageno$pageno = 1; //若pageno比1小,则把它设置为1
}
if($pageno>$pagecount) {
$pageno = $pagecount; //若pageno比总共的页数大,则把它设置为最后一页
}
if($pageno>0) {
$href = eregi_replace("%2f","/",urlencode($PHP_SELF));//把$PHP_SELF转换为可以在URL上使用的字符串,这样的话就可以处理中文目录或中文文件名
if($pageno>1){//显示上一页的裢接
echo "上一页 ";
}
else{
echo "上一页 ";
}
for($i=1;$iecho "" . $i . " ";
}
echo $pageno . " ";
for($i++;$iecho "" . $i . " ";
}
if($pagenoecho "下一页 ";
}
else{
echo "下一页 ";
}

$offset = ($pageno-1) * $pagesize;//算出本页第一条记录在整个表中的位置(第一条记录为0)
$sql = "select * from pagetest LIMIT $offset,$pagesize";//生成查询本页数据的SQL语句
$rst = mysql_query($sql);//查询本页数据
$num_fields = mysql_num_fields($rst);//取得字段总数
$i = 0;
while($i$fields[$i] = mysql_field_name($rst,$i);//取得第i+1个字段的名字
$i++;
}
echo "";//开始输出表格
echo "";
reset($fields);
while(list(,$field_name)=each($fields)){//显示字段名称
echo "";
}
echo "";
while($row=mysql_fetch_array($rst)){//显示本页数据
echo "";
reset($fields);
while(list(,$field_name)=each($fields)){//显示每个字段的值
$field_value = $row[$field_name];
if($field_value==""){
echo "";
}
else{
echo "";
}
}
echo "";
}
echo "
$field_name
$field_value
";//表格输出结束
mysql_free_result($rst) or die("无法释放result资源!");//释放result资源
}
else{
echo "目前该表中没有任何数据!";
}

mysql_close($server) or die("无法与服务器断开连接!");//断开连接并释放资源
?>
  从这个例子可以看出,PHP的语法结构很象C语言,并易于掌握。而且PHP的跨平台特性让程序无论在WINDOWS平台还是LINUX、UNIX系统都能运行自如。笔者编写PHP程序是在WINNT4中,然后上传到UNIX系统运行,从未发现兼容性的问题。
  到目前为止,无论在个人网站还是在企业网站上,以上4种技术中以PHP的应用最为广泛。
  以上4种技术,皆在制作动态网页上各显神通。至于选择哪种技术,取决于制作者的爱好和技术储备。对于广大个人主页的爱好者、制作者来说,笔者建议尽量少用难度较大、上手较慢的CGI技术。如果您是"微软"的拥趸,采用ASP技术会让您得心应手;如果是LINUX的追求者,运用PHP技术在目前是最恰当、最明智的选择。此外,不要忽略了JSP技术,据说它是未来最有发展前途的动态网站技术,但是在学JSP之前,必须掌握JAVA技术。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles