Home php教程 php手册 PHP脚本与数据库功能详解(下)

PHP脚本与数据库功能详解(下)

Jun 13, 2016 am 11:39 AM
php Down and keep use Function Will database document Script Detailed explanation

在上一篇:PHP脚本与数据库功能详解(中),利用PHP将文件保存到数据库,下面我们接着说用类加快PHP的数据库开发
  数据库的访问函数较多,使用不当会降低效率,甚至导致错误。而PHP的本身就是开放的和可扩充的,很多人为它开发各种功能的源代码。每一个PHP程序员都应该善于继承他人的成果,节省时间和精力。站在巨人的肩膀上,才能看得更远。当然,你也可以把你的代码共享出来,体会自己的劳动被承认和创造价值而带来的成就感。

  使用数据库类,可以使我们完全不必考虑具体的数据库类型,而专注于程序的开发上。

  众多的开发工具包中,PHPLib是性能较稳定、功能较完善的一个。PHPLib可以在http://phplib.netuse.de/ 获得。它包含了数据库的支持类。以MySQL数据库为例,PHPLib自带名为DB_Sql的类。它包装了数据库的连接、查询、取结果、数据库表的遍历等功能。

  使用数据库类,可以使我们完全不必考虑具体的数据库类型,而专注于程序的开发上。即使数据库系统类型换了,程序代码也不用改。同时,数据库类提供了完整而健壮的数据库访问方法,这可能是使用类的包装的最大的优势了。
  下面,我们就使用PHPLib提供的数据库类,来访问我们刚才建立的数据库,并对内容进行显示。

  〈?

  require "db_mysql.php";

  //包含数据库类的生成文件

  $db=new DB_Sql;

  //声明数据库类的实例

  $db-〉connect("ResumeDB","localhost", "root", "");

  //连接数据库服务器

  //提供的参数依次为:数据库名,主机名,用户名,用户密码

  if ($db-〉Link_ID)

  //判断是否正确建立连接

  {

   $db-〉query("select ID,Name,Intro FROM Resume");

  //查询

   if ($db-〉nf())

  //判断结果集是否为空

   {

   while ($db-〉next_record())

  //取得下一行记录值,直到记录集内容取完

   {

   echo "ID:", $db-〉f("ID"); //f()函数返回当前记录某个子段的值

   echo "〈br〉";

   echo "姓名:";

   $db-〉p("Name");

  //p()函数直接打印某个子段的值

  //等价于echo $db-〉f("name")

   echo "〈br〉";

   echo "简介:";

   echo $db-〉f("Intro");

   echo "〈br〉";

   echo "〈a href= "download.php?ID=".$db-〉f("ID").""〉查看Word文档〈/a〉";

   echo "〈br〉〈hr〉";

   }

   }

   $db-〉free ();

  //释放资源

  }

  ?〉

  从上面的流程可以看出,用类访问数据库的方法和直接访问数据库的方法基本相同。不同的是,这里我们调用的方法都是类的方法,而不是具体针对某种数据库的函数。由于代码和具体数据库类型的分离,使得当数据库系统改变的时候,我们不用改变程序代码,只要改变基类的实现方法即可。

  如果结合使用PHPLib模板进行设计的话,即可实现程序与显示的分离。也将使得程序结构清晰,网页美工设计制作方便。

  • 共2页:
  • 上一页
  • 1
  • 2
  • 下一页
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis 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.

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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles